How to convert seconds to hh:mm:ss in a sql query

Posted on 2003-02-27
Medium Priority
Last Modified: 2006-11-17

I'm new to this but can anyone help me with how to convert seconds to hours:minutes:seconds in a sql query.

My query is like this:

"select duration from tablename"

The column duration returns seconds and I want to format it in to hh:mm:ss in the statement.

Question by:Toti
  • 2

Expert Comment

ID: 8034450
declare @i int

select  @i = 1234

select  convert( char(8), dateadd( ss, @i, '00:00:00' ), 108 )

Accepted Solution

Tyip earned 200 total points
ID: 8034465
in your case, it will be

select convert( char(8), dateadd( ss, duraction, '00:00:00' ), 108 )
from tablename

Author Comment

ID: 8180505
Excellent answer and does the job.  

However if I want to add number of days in front of hours so the display would be like this dd:hh:mm:ss or for that matter yy:mm:dd:hh:mm:ss.  What would that sql script look like?

The thing is that I'm using this script on Call Detail Records in a Phone System for a summation of call time.  In a year period total talk time would be measured in the above scale.

Regards Toti

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question