Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can convert seconds to days by dividing by 86400</p> <p>You can convert seconds to hours by dividing by 3600, but you need to get the remainder (by subtracting off the total days converted to hours)</p> <p>You can convert seconds to minutes by dividing by 60, but you need to get the remainder (by subtracting off the total hours converted to minutes)</p> <p>Seconds you can just report, but like minutes you want to only report the remainder of seconds (by sutracting off the total minutes converted to seconds)</p> <pre><code>SELECT FLOOR( UpTime / 86400 ) AS DAYS , FLOOR( ( UpTime / 3600 ) - FLOOR( UpTime / 86400 ) * 24 ) AS HOURS , FLOOR( ( UpTime / 60 ) - FLOOR( UpTime / 3600 ) * 60 ) AS MINUTES , UpTime - FLOOR( UpTime / 60 ) * 60 AS SECONDS FROM ( SELECT 269272 AS UpTime ) AS X </code></pre> <p><code>269272</code> represents 3 days (259200 seconds), 2 hours (7200 seconds), 47 minutes (2820 seconds) and 52 seconds.</p> <p>This query produces:</p> <pre class="lang-none prettyprint-override"><code>| DAYS | HOURS | MINUTES | SECONDS | ------------------------------------ | 3 | 2 | 47 | 52 | </code></pre> <p>Substituting 125 (2 minutes, 5 seconds) for 259200 will produce: </p> <pre><code>| DAYS | HOURS | MINUTES | SECONDS | ------------------------------------ | 0 | 0 | 2 | 5 | </code></pre> <p>To convert this to a string representation, you can use SQL Server 2012's FORMAT function:</p> <pre><code>SELECT CASE WHEN DAYS &gt; 0 THEN FORMAT( DAYS, '##' ) + ' Day(s) ' + FORMAT( HOURS, '##' ) + ' Hour(s)' ELSE FORMAT( HOURS, '##' ) + ':' + FORMAT( MINUTES, '##' ) + ' Hour(s) Minute(s)' END AS UpTimeString FROM ( SELECT FLOOR( UpTime / 86400 ) AS DAYS , FLOOR( ( UpTime / 3600 ) - FLOOR( UpTime / 86400 ) * 24 ) AS HOURS , FLOOR( ( UpTime / 60 ) - FLOOR( UpTime / 3600 ) * 60 ) AS MINUTES , UpTime - FLOOR( UpTime / 60 ) * 60 AS SECONDS FROM ( SELECT 125 AS UpTime ) AS X ) AS UptimeSubselect </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload