SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Convert seconds to a timestring

By Wilfred van Dijk,

If you use datediff to determine how log something is running, you'll have to make the choice how to represent it. If you choose minutes, SQL will round the datediff time. If you choose seconds, you'll have to determine how many minutes + some seconds this was (try 302 seconds)

Since I'm lazy, I wrote this function which converts a number of seconds to a 'human' time format. for example 302 seconds is represented as '00:05:02', meaning 5 minutes and 2 seconds.

Note: If this function is behaving strange, copy/paste the code into an editor and replace "goofy" characters

NOTE: Thanks to Nigel, this function is replaced by a fantastic use of CONVERT/DATEADD)



Total article views: 1389 | Views in the last 30 days: 1
Related Articles

Convert DateDiff into Hours, Minutes, Seconds, Milliseconds

I have the hours, minutes, and seconds...but need the milliseconds.


Function to Round or Truncate DateTime

This Function is used to round to Second, Minute, Hour or Day or to Truncate to Second, Minute, Hou...


SQLBIGeek’s Function Friday – Convert Job Duration to Seconds

Brian K. McDonald SQLBIGeek Twitter: @briankmcdonald Welcome to the second post of my “...


OPENROWSET select statement performance -- runs in 4 seconds on test server vs. 3 minutes on production server.

OPENROWSET select statement performance -- runs in 4 seconds on test server vs. 3 minutes on product...


Julian Date Conversion Function Issue

Conversion Function not returning Seconds on date