Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL server, Converting Seconds to Minutes, Hours, Days

I have a database column containing an integer value that represents a systems up time in seconds. I'd really like a query to be able to show me that up time in a easy to read format day(s) hour(s) minute(s) but I'm not quite sure how to do it. A lot of examples I've found appear to use parameters as an example but never much of how to use it in a select function.

I need the time to be the same as what's displayed on a website too. I tried one query earlier and its added days and removed minutes. Can anyone help me out?

Source data:

PDT0014 6141
PDT0008 4990
PDT0024 840227
PDT0033 2301
PDT0035 5439
PDT0005 3434
PDT0019 5482

Sample code:

SELECT tblAssets.AssetName, 
(case when tblAssets.Uptime> (24*60*60) 
        then 
            cast(datepart(day,datediff(dd, 0, dateadd(second, tblAssets.Uptime, 0))) as varchar(4))
        +   ' Day(s) ' + convert(varchar(2), dateadd(second, tblAssets.Uptime, 0), 108) +' Hour(s)' 
    else
            convert(varchar(5), dateadd(second, tblAssets.Uptime, 0), 108) + ' Hour(s) Minute(s) ' 
    end) AS Uptime
FROM tblAssets

Desired Query Output:

PDT0014 01:42 Hour(s) Minute(s) 
PDT0008 01:23 Hour(s) Minute(s) 
PDT0024 10 Day(s) 17 Hour(s)
PDT0033 00:38 Hour(s) Minute(s) 
PDT0035 01:30 Hour(s) Minute(s) 
PDT0005 00:57 Hour(s) Minute(s) 
PDT0019 01:31 Hour(s) Minute(s)
like image 477
Tinydan Avatar asked Oct 10 '13 14:10

Tinydan


2 Answers

Depending on the output you want:

DECLARE @s INT = 139905;

SELECT                CONVERT(VARCHAR(12), @s /60/60/24) + ' Day(s), ' 
  +                   CONVERT(VARCHAR(12), @s /60/60 % 24) 
  + ':' + RIGHT('0' + CONVERT(VARCHAR(2),  @s /60 % 60), 2) 
  + ':' + RIGHT('0' + CONVERT(VARCHAR(2),  @s % 60), 2);

Result:

1 Day(s), 14:51:45

Or:

DECLARE @s INT = 139905;

SELECT 
    CONVERT(VARCHAR(12), @s /60/60/24)   + ' Day(s), ' 
  + CONVERT(VARCHAR(12), @s /60/60 % 24) + ' Hour(s), '
  + CONVERT(VARCHAR(2),  @s /60 % 60)    + ' Minute(s), ' 
  + CONVERT(VARCHAR(2),  @s % 60)        + ' Second(s).';

Result:

1 Day(s), 14 Hour(s), 51 Minute(s), 45 Second(s).

You can replace 60/60/24 with 86400 etc. but I find it better self-documenting if you leave in the /seconds/minutes/hours calculations. And if you are going against a table, just use column_name in place of @s.

like image 193
Aaron Bertrand Avatar answered Sep 20 '22 23:09

Aaron Bertrand


I tend to use:

CAST(FLOOR(seconds / 86400) AS VARCHAR(10))+'d ' +
    CONVERT(VARCHAR(5), DATEADD(SECOND, Seconds, '19000101'), 8)

The top part just gets your days as an integer, the bottom uses SQL-Server's convert to convert a date into a varchar in the format HH:mm:ss after converting seconds into a date.

e.g.

SELECT  Formatted = CAST(FLOOR(seconds / 86400) AS VARCHAR(10))+'d ' +
                        CONVERT(VARCHAR(5), DATEADD(SECOND, Seconds, '19000101'), 8),
        Seconds
FROM    (   SELECT  TOP 10 
                    Seconds = (ROW_NUMBER() OVER (ORDER BY Object_ID) * 40000)
            FROM    sys.all_Objects
            ORDER BY Object_ID
        ) S

Example on SQL Fiddle

N.B. Change CONVERT(VARCHAR(5), DATEADD(.. to CONVERT(VARCHAR(8), DATEADD(.. to keep the seconds in the result

EDIT

If you don't want seconds and need to round to the nearest minute rather than truncate you can use:

SELECT  Formatted = CAST(FLOOR(ROUND(Seconds / 60.0, 0) * 60 / 86400) AS VARCHAR(10))+'d ' +
                        CONVERT(VARCHAR(5), DATEADD(SECOND, ROUND(Seconds / 60.0, 0) * 60, '19000101'), 8),
        Seconds
FROM    (   SELECT  Seconds = 3899
        ) S

I have just replaced each reference to the column seconds with:

ROUND(Seconds / 60.0, 0) * 60

So before doing the conversion rounding your seconds value to the nearest minute

like image 39
GarethD Avatar answered Sep 22 '22 23:09

GarethD