SQL: Is there a more efficient way to calculate elapsed hours, minutes, seconds?

Posted by hamlin11 on Stack Overflow See other posts from Stack Overflow or by hamlin11
Published on 2010-05-22T03:34:32Z Indexed on 2010/05/22 3:40 UTC
Read the original article Hit count: 178

Filed under:

I'm using Computed Columns to provide me with the following information:

Hours, Minutes, and Seconds between a Start DateTime and Finish DateTime (Where Minutes and Seconds are between 0 and 59 and Hours can be any value 0 or greater)

Computed Column for Seconds:

datediff(second,[Start],[Finish]) % 60

Computed Column for Minutes:

floor(datediff(second,[Start],[Finish]) / 60.0) % 60

Computed Column for Hours:

floor(datediff(second,[Start],[Finish]) / 3600.0)

Here's the table for reference

alt text

Note: I'm also calculating TotalElapsedSeconds, TotalElapsedMinutes, and TotalElapsedHours in other computed columns, but those are easy. I just feel like I might be missing out on a nice built in function in SQL.

© Stack Overflow or respective owner

Related posts about sql