SQL SERVER:获取两个日期之间的总天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6068017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL SERVER: Get total days between two dates
提问by Pod Mays
I'm trying to get the total number of days between two days:
我试图获得两天之间的总天数:
1/1/2011
3/1/2011
RETURN
62
Is it possible to do in SQL Server?
是否可以在 SQL Server 中执行?
回答by Will A
PRINT DATEDIFF(DAY, '1/1/2011', '3/1/2011')
will give you what you're after.
PRINT DATEDIFF(DAY, '1/1/2011', '3/1/2011')
会给你你想要的。
This gives the number of times the midnight boundary is crossed between the two dates. You may decide to need to add one to this if you're including both dates in the count - or subtract one if you don't want to include either date.
这给出了两个日期之间跨越午夜边界的次数。如果您将两个日期都包括在计数中,您可能决定需要为此添加一个 - 如果您不想包括任何一个日期,则减去一个。
回答by Khepri
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);
回答by jams
回答by Mitch Wheat
回答by cakiran
Another date format
另一种日期格式
select datediff(day,'20110101','20110301')
回答by VMAtm
SELECT DATEDIFF(day, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');
回答by bulbul bd
This is working for me -
这对我有用-
SELECT DATEDIFF(DAY, startdate, enddate) AS DayCount
Example : SELECT DATEDIFF(DAY, '11/30/2019', GETDATE()) AS DayCount
回答by Bha15
if you want to do same thing Store Procedure then you need to apply below code.
如果你想做同样的事情 Store Procedure 那么你需要应用下面的代码。
select (datediff(dd,'+CHAR(39)+ convert(varchar(10),@FromDate ,101)+
CHAR(39)+','+CHAR(39)+ convert(varchar(10),@ToDate ,101) + CHAR(39) +'))
Daysdiff
where @fromdate and @todate is Parameter of the SP
其中@fromdate 和@todate 是 SP 的参数
回答by JIYAUL MUSTAPHA
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*/