Date Subtraction

  • I have a table with two columns, both of which are dates. What would the script be to subtract one from the other and return just the number of days difference between the two?

    Thanks...Nali

  • Checkout the datediff function

  • also the date add function accepts negative numbers, so if you need something like "15 days back from today":

    SELECT DATEADD(day, -14, getdate()) AS TwoWeeksAgo

    results:

    TwoWeeksAgo                                           

    ------------------------------------------------------

    2007-02-06 17:03:39.997

    (1 row(s) affected)

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • To express the difference between two dates in days...

    DATEPART(dd,(DATEDIFF(@startdate,@enddate)))

    or

    DAY(DATEDIFF((@startdate,@enddate))

    Both are equivalent - it follows the SQL tradition of there always being more than one way to solve a problem

  • Yeah, both are equivalent: they are both syntax errors.

    How about: DATEDIFF(DAY, @startdate, @enddate)

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply