Solved

# SQL SERVER  + convert seconds to hours and minutes

Posted on 2007-10-15
11,589 Views
How do you within a query using sql 2000 convert a number in seconds....into hours and minutes...

Thanks
0
Question by:Robb Hill

LVL 142

Accepted Solution

like this, for example:

select convert(varchar(10), dateadd(second, <number of seconds>, 0) , 8)
0

LVL 44

Assisted Solution

Will give you HH:MM:SS result. Converting to varchar(5) will drop off the seconds.
0

LVL 11

Author Comment

what is the difference between teh two answers....here...i ran it..and am getting the same results for both formulas
0

LVL 11

Assisted Solution

select convert(varchar(10), dateadd(second, <number of seconds>, 0) , 8)
Gives you the format hh:mi:ss

Gives you the format hh:mi:ss:mmm(24h)  but it's truncating off the milliseconds at the end because you only converted to varchar(8).  If you converted to varchar(11) you'd see the milliseconds.
0

LVL 68

Assisted Solution

Those methods will work as long as you *always have less than 24 hours*.  If you could have more than 24 hours, you need to do something like this:

DECLARE @NumberOfSeconds INT
SET @NumberOfSeconds = (25 * 60 * 60) /*hrs*/ + (7 * 60) /*mins*/ + 57 /*left-over secs*/
SELECT CAST(@NumberOfSeconds / 3600 AS VARCHAR(3)) + ':' +
RIGHT('0' + CAST(@NumberOfSeconds % 3600 / 60 AS VARCHAR(2)), 2)

If you need to round, replace @NumberOfSeconds with (@NumberOfSeconds + 30) in the calcs.
0

## Featured Post

### Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

#### Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!