我试图获得两天之间的总天数:
1/1/2011
3/1/2011
RETURN
62
可以在SQL Server中执行吗?
答案 0 :(得分:253)
PRINT DATEDIFF(DAY, '1/1/2011', '3/1/2011')
将为您提供您所追求的目标。
这给出了两个日期之间跨越午夜边界的次数。如果您在计数中包含两个日期,则可能决定需要添加一个 - 或者如果您不想包含任何一个日期,则减去一个日期。
答案 1 :(得分:35)
DECLARE @startdate datetime2 = '2007-05-05 12:10:09.3312722';
DECLARE @enddate datetime2 = '2009-05-04 12:10:09.3312722';
SELECT DATEDIFF(day, @startdate, @enddate);
答案 2 :(得分:15)
请参阅DateDiff:
DECLARE @startdate date = '2011/1/1'
DECLARE @enddate date = '2011/3/1'
SELECT DATEDIFF(day, @startdate, @enddate)
答案 3 :(得分:14)
您可以尝试此MSDN link
DATEDIFF ( datepart , startdate , enddate )
SELECT DATEDIFF(DAY, '1/1/2011', '3/1/2011')
答案 4 :(得分:10)
另一种日期格式
select datediff(day,'20110101','20110301')
答案 5 :(得分:4)
SELECT DATEDIFF(day, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
答案 6 :(得分:0)
如果你想做同样的事情存储过程,那么你需要应用下面的代码。
select (datediff(dd,'+CHAR(39)+ convert(varchar(10),@FromDate ,101)+
CHAR(39)+','+CHAR(39)+ convert(varchar(10),@ToDate ,101) + CHAR(39) +'))
Daysdiff
其中@fromdate和@todate是SP的参数
答案 7 :(得分:0)
这对我有用-
SELECT DATEDIFF(DAY, startdate, enddate) AS DayCount
Example : SELECT DATEDIFF(DAY, '11/30/2019', GETDATE()) AS DayCount
答案 8 :(得分:-1)
DECLARE @FDate DATETIME='05-05-2019' /*This is first date*/
GETDATE()/*This is Current date*/
SELECT (DATEDIFF(DAY,(@LastDate),GETDATE())) As DifferenceDays/*this query will return no of days between firstdate & Current date*/