Solved

Difference Between Two Dates Stored Procedure SQL

Posted on 2009-05-18
4
451 Views
Last Modified: 2012-05-07
Hi

I am trying to do my first stored procedure in SQL Server Management 2005 Express. I basically want to take two dates from a table of batch numbers and calculate the time span between the two dates. (The dates will be the first and last datetime that the batch number was recorded in table).

I have my code so far, but it does not compile - throwing incorrect syntax error on the line:

set @firstdate = SELECT top 1 datetimecol from Test_01.Table_1 where batchcol = @batchnumber order by datetimecol ASC

Even then I am not sure I am going the correct route. any hints / opinions would be appreciated.

Cheers

Neil Whitley
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE TimeDifferenceSecs 
	-- Add the parameters for the stored procedure here
	@batchnumber int = 99, 
	@vessel varchar(50) = "??",
	@firstdate as datetime,
	@lastdate as datetime,
	@difference as datetime
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
 
    -- Insert statements for procedure here
	set @firstdate = SELECT top 1 datetimecol from Test_01.Table_1 where batchcol = @batchnumber order by datetimecol ASC
 
	set @lastdate = SELECT top 1 datetimecol from Test_01.Table_1 where batchcol = @batchnumber order by datetimecol DES
 
	set @difference = Datediff("s",@firstdate,@lastdate)
 
END
GO

Open in new window

0
Comment
Question by:silchester
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
ID: 24412553
i would do this instead

select datediff(s, min(datetimecol), max(datetimecol))
from test_01.dbo.table_1
where batchcol = @batchcol
0
 

Author Comment

by:silchester
ID: 24412770
Cheers :-) But a stupid question now is how the hell do I run this and see results? I have executed it but there are no results to be seen; I have set SQL to output results to grid, but do not even see a grid?

Sorry for the stupid question.

Neil
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24412795
you'll need to call the procedure and pass parameters to it.

http://www.sql-server-performance.com/articles/dba/stored_procedures_basics_p1.aspx
0
 

Author Closing Comment

by:silchester
ID: 31582633
Thanks for that - have sorted the results grid query too.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question