INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Date and time Tips and tricks

Converting Seconds to HH:MM:SS by SQLBill
Posted: 28 Dec 05 (Edited 4 Jan 06)

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

Back to Microsoft SQL Server: Programming FAQ Index
Back to Microsoft SQL Server: Programming Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close