Search code examples
sql-server-2005t-sql

Calculating timespan with t-sql


Given two date/times:

@start_date = '2009-04-15 10:24:00.000'
@end_date = '2009-04-16 19:43:01.000'

Is it possible to calculate the time elapsed between the two dates in the following format

1d 9h 19m


Solution

  • You can get the difference between the two dates to whatever resolution you want (in your example, minutes):

    DATEDIFF(minute, @start_date, @end_date)
    

    From there it's a simple matter of dividing minutes into hours and hours into days and modding the remainder.