Cummulate Date Difference Calculation

0

Please login or register to vote for this query.

(click on this box to dismiss)

Lists the difference between dates in years, months, days

Stack Overflow

Q&A for programmers

-- How Old are You
-- Lists the difference between dates in years, months, days

WITH dates AS (
  SELECT '1996-03-15' AS startdate,
         GETDATE() AS enddate)
SELECT DATEDIFF(yy, t.startdate, t.enddate) AS years,
DATEDIFF(mm, DATEADD(yy, DATEDIFF(yy, t.startdate, t.enddate), t.startdate), t.enddate) AS months,
DATEDIFF(dd,
         DATEADD(mm, DATEDIFF(mm, DATEADD(yy, DATEDIFF(yy, t.startdate, t.enddate), t.startdate), t.enddate), DATEADD(yy, DATEDIFF(yy, t.startdate, t.enddate), t.startdate)),
         t.enddate) AS days
  FROM dates t

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results
:records returned in :time ms:cached