views:

171

answers:

5

hi guys,

I have 2 dates. I want to get number of days between 2 dates in storedprocedure.

+1  A: 

How about using the dateDiff function ?

eg

DECLARE @Dt INT
SET @Dt = DATEDIFF(dd,@StartDate,@EndDate)

should do the trick ?

Or did I miss something ?

no_one
+11  A: 

DateDiff function should do what you need

declare @var1 Datetime
declare @var2 Datetime

set @var1 = '2009-04-01'
set @var2 = '2009-04-16'

SELECT datediff(day,@var1, @var2 )
Eoin Campbell
@var1, @var2 = bad names :)
Filip Ekberg
lol... yeah I've been bitten by the "throw-code-samples-together-quickly-and-uglily-for-StackOverFlow" bug ;-)
Eoin Campbell
+1  A: 
USE tempdb

DECLARE @DATE1 datetime

DECLARE @DATE2 datetime

SET @DATE1 = '01/01/2000'

SET @DATE2 = '02/01/2000'

SELECT DATEDIFF(day, @DATE1, @DATE2)
Sonoride
A: 

DATEDIFF is the way to do it

A: 

Note that DATEDIFF only concerns itself with the date portion. If times are involved, a converted subtraction might yield better results.

DECLARE @start DATETIME
DECLARE @end DATETIME
SET @start = '20090514 00:00:00'
SET @end = '20090514 23:59:59'
PRINT CONVERT(FLOAT, (@end-@start))  -- 0.999988
PRINT DATEDIFF(DAY,@start,@end)      -- 0
Hafthor