SQL SERVER  + convert seconds to hours and minutes

Posted on 2007-10-15
Last Modified: 2010-01-19
How do you within a query using sql 2000 convert a number in seconds....into hours and minutes...

Question by:Robb Hill
    LVL 142

    Accepted Solution

    like this, for example:

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

    Assisted Solution

    Select Convert(varchar(8),Dateadd(s,NumberOfSeconds,0),14) From MyTable

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

    Author Comment

    by:Robb Hill
    what is the difference between teh two ran it..and am getting the same results for both formulas
    LVL 11

    Assisted Solution

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

    Select Convert(varchar(8),Dateadd(s,NumberOfSeconds,0),14) From MyTable
    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.
    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In this article—a derivative of my blog post (—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.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now