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
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
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.
datediff(datepart, date1, date2);
Rex's answer is more complete, upmodding him.
DATEDIFF can return unintuitive values. For example, the two dates below differ by one second yet DATEDIFF with the parameters below and interpreted as others have interpreted it above returns 1 year:
SELECT DATEDIFF(year, '2005-12-31 23:59:59', '2006-01-01 00:00:00')
Look at the MSDN documentation for DATEDIFF to understand how it works.
CONVERT(varchar,(@end_date-@start_date),108)
This'll give it to you as HH:MM:SS
Cheers