要获得2个日期之间的差异

时间:2023-01-17 21:31:05

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

我有2个约会。我想获得storedprocedure中两个日期之间的天数。

5 个解决方案

#1


DateDiff function should do what you need

DateDiff函数应该做你需要的

declare @var1 Datetime
declare @var2 Datetime

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

SELECT datediff(day,@var1, @var2 )

#2


How about using the dateDiff function ?

如何使用dateDiff函数?

eg

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

should do the trick ?

应该做的伎俩?

Or did I miss something ?

还是我错过了什么?

#3


USE tempdb

DECLARE @DATE1 datetime

DECLARE @DATE2 datetime

SET @DATE1 = '01/01/2000'

SET @DATE2 = '02/01/2000'

SELECT DATEDIFF(day, @DATE1, @DATE2)

#4


DATEDIFF is the way to do it

DATEDIFF就是这样做的

#5


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

请注意,DATEDIFF仅关注日期部分。如果涉及时间,则转换的减法可能会产生更好的结果。

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

#1


DateDiff function should do what you need

DateDiff函数应该做你需要的

declare @var1 Datetime
declare @var2 Datetime

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

SELECT datediff(day,@var1, @var2 )

#2


How about using the dateDiff function ?

如何使用dateDiff函数?

eg

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

should do the trick ?

应该做的伎俩?

Or did I miss something ?

还是我错过了什么?

#3


USE tempdb

DECLARE @DATE1 datetime

DECLARE @DATE2 datetime

SET @DATE1 = '01/01/2000'

SET @DATE2 = '02/01/2000'

SELECT DATEDIFF(day, @DATE1, @DATE2)

#4


DATEDIFF is the way to do it

DATEDIFF就是这样做的

#5


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

请注意,DATEDIFF仅关注日期部分。如果涉及时间,则转换的减法可能会产生更好的结果。

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