Given two datetimes, how can I query, using t-sql, the exact day, hour and minute difference between them?
d1 = 2011-02-08 07:52
d2 = 2011-02-14 15:34
The correct answer is 5 days, 7 hours and 42 minutes.
Using Datediff with dd, hour, minute arguments returns 6 days.
Thank you for help.
The difference between February 8th at 7:52 AM and February 14th at 3:34 PM is indeed 6 days, 7 hours, and 42 minutes.
Check out Jeff Moden's outstanding post on time differences for a way to calculate the difference in various formats. He did a great job explaining the concept, I think. Later on in the thread, GSquared has another method for getting the values, and it looks like this:
This query has some problems with edge cases, so beware if your query needs to span more than one year.
answered Feb 14, 2011 at 04:48 PM