INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

HANDLE


PASSWORD
Remember Me
Forgot Password?

Come Join Us!

  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • Turn Off Ad Banners
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

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

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...Keep up the good work - excellent site - i'd been looking for something like this for ages !..."

Geography

Where in the world do Tek-Tips members come from?

Microsoft SQL Server: Programming FAQ

Date and time Tips and tricks

Converting Seconds to HH:MM:SS
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 FAQ Archive
Email This FAQ To A Friend

My Archive