|
Given two datetimes, how can I query, using t-sql, the exact day, hour and minute difference between them?
(comments are locked)
|
|
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. Wow! I'd lost track of that particular thread. Thanks, Kevin! Maybe I should turn it into an "SQL Spackle" article.
Feb 14 '11 at 06:21 PM
Jeff Moden
That might be a good one, yeah. I normally would use C# for date comparisons (that's what TimeSpan is for, after all), but this seems like a tailor-made short article: it won't come up often, but when it does, it's good to have something in the Briefcase that solves the problem.
Feb 15 '11 at 04:14 AM
Kevin Feasel
(comments are locked)
|

