Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting Seconds to HH:MM:SS

Date and time Tips and tricks

Converting Seconds to HH:MM:SS

by  SQLBill  Posted    (Edited  )
So you have your time in seconds and you want to display it as hours, minutes, and seconds. This probably isn't the only way to do it, but it's the way I came up with. If you have another method (especially if it's better) send it to me by replying to this FAQ. I'll include it and give you proper credit. Or create another FAQ with your solution.
Code:
DECLARE @mytime INT
DECLARE @myhour INT
DECLARE @mymin INT
DECLARE @mysec INT
SET @mytime = 3601  --or whatever value of seconds you have
SET @myhour = (SELECT @mytime/3600)
SET @mytime = (SELECT @mytime%3600)
SET @mymin = (SELECT @mytime/60)
SET @mysec = (SELECT @mytime%60)
SELECT CAST(@myhour AS VARCHAR(2)) + ':' + CAST(@mymin AS VARCHAR(2)) + ':' + CAST(@mysec AS VARCHAR(2))
That won't add the leading zeros.

You can replace the SET @mytime = 3601 line with something like: SET @mytime = (SELECT DATEDIFF(ss, GETDATE()-100, GETDATE())) or whatever you need for your purposes.

-SQLBill
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top