SQL SERVER: Get total days between two dates

SqlSql ServerSql Server-2008Date

Sql Problem Overview


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 Solutions


Solution 1 - Sql

PRINT DATEDIFF(DAY, '1/1/2011', '3/1/2011') will give you what you're after.

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.

Solution 2 - Sql

SQL Server DateDiff

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);

Solution 3 - Sql

You can try this MSDN link

DATEDIFF ( datepart , startdate , enddate )
SELECT DATEDIFF(DAY, '1/1/2011', '3/1/2011')

Solution 4 - Sql

See DateDiff:

DECLARE @startdate date = '2011/1/1'
DECLARE @enddate date = '2011/3/1'
SELECT DATEDIFF(day, @startdate, @enddate)

Solution 5 - Sql

Another date format

select datediff(day,'20110101','20110301')

Solution 6 - Sql

SELECT DATEDIFF(day, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000');

Solution 7 - Sql

This is working for me -

SELECT DATEDIFF(DAY, startdate, enddate) AS DayCount

Example : SELECT DATEDIFF(DAY, '11/30/2019', GETDATE()) AS DayCount

Solution 8 - Sql

if you want to do same thing Store Procedure then you need to apply below code.

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

Solution 9 - Sql

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*/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPod MaysView Question on Stackoverflow
Solution 1 - SqlWill AView Answer on Stackoverflow
Solution 2 - SqlKhepriView Answer on Stackoverflow
Solution 3 - SqljamsView Answer on Stackoverflow
Solution 4 - SqlMitch WheatView Answer on Stackoverflow
Solution 5 - SqlcakiranView Answer on Stackoverflow
Solution 6 - SqlVMAtmView Answer on Stackoverflow
Solution 7 - Sqlbulbul bdView Answer on Stackoverflow
Solution 8 - SqlBha15View Answer on Stackoverflow
Solution 9 - SqlCodeView Answer on Stackoverflow