SQL SERVER: Get total days between two dates

SQL SERVER: Get total days between two dates

Asked on December 18, 2018 in SQL Server.
Add Comment


  • 5 Answer(s)

    That is very simple to get the number of days between two dates,

    This code give us what we are after,

    PRINT DATEDIFF(DAY, '1/1/2011', '3/1/2011')
    

    The code will give us the count of midnight boundary is crossed between the two dates in . Add one to this if including both dates in the count – or subtract one if don’t want to include either date.

    Answered on December 18, 2018.
    Add Comment

    This should works, try this:

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

    For more references SQL Server DateDiff.

    Answered on December 18, 2018.
    Add Comment

    Use this code,

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

    For additional information for DateDiff.

    Answered on December 18, 2018.
    Add Comment
    DATEDIFF ( datepart , startdate , enddate )
    SELECT DATEDIFF(DAY, '1/1/2011', '3/1/2011')
    Answered on January 14, 2019.
    Add Comment
    SELECT DATEDIFF(hour, ‘2017/08/25 07:00’, ‘2017/08/25 12:45’)
    Answered on January 14, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.