我想要获取两个日期之间的总天数:
1/1/2011
3/1/2011
RETURN
62
在SQL Server中是否可能实现?
我想要获取两个日期之间的总天数:
1/1/2011
3/1/2011
RETURN
62
在SQL Server中是否可能实现?
PRINT DATEDIFF(DAY, '1/1/2011', '3/1/2011')
会给你想要的结果。
这将给出两个日期之间跨越午夜边界的次数。如果您要计算两个日期在内,可能需要将其加一 - 如果您不想包括任何日期,则需要减去一。
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);
您可以尝试这个MSDN链接
DATEDIFF ( datepart , startdate , enddate )
SELECT DATEDIFF(DAY, '1/1/2011', '3/1/2011')
请参阅DateDiff:
DECLARE @startdate date = '2011/1/1'
DECLARE @enddate date = '2011/3/1'
SELECT DATEDIFF(day, @startdate, @enddate)
另一种日期格式
select datediff(day,'20110101','20110301')
SELECT DATEDIFF(day, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
这对我来说是有效的 -
SELECT DATEDIFF(DAY, startdate, enddate) AS DayCount
Example : SELECT DATEDIFF(DAY, '11/30/2019', GETDATE()) AS DayCount
如果你想要做相同的存储过程,那么你需要应用以下代码。
select (datediff(dd,'+CHAR(39)+ convert(varchar(10),@FromDate ,101)+
CHAR(39)+','+CHAR(39)+ convert(varchar(10),@ToDate ,101) + CHAR(39) +'))
Daysdiff
@fromdate和@todate是存储过程的参数
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*/