SDU Tools: Seconds to Duration in T-SQL

SDU Tools: Seconds to Duration in T-SQL

There are a few places in T-SQL where instead of a time value, you need to work with a duration.

An example is if I want to sleep for a short period. In T-SQL, we can do that with the WAITFOR statement, used like this:

This command would cause SQL Server to wait for 10 minutes. One of the challenges though, is how do I create this duration string in a programmatic way? For example, how do I create the string ‘00:02:00’ if I’m starting with a value of 120 seconds?

One of our free SDU Tools for developers and DBAs does just this. You can see it being used in the main image above.

SecondsToDuration converts a number of seconds to a SQL Server duration string (similar to programming identifiers). Note that the value must be less than 24 hours (between 0 and 86399) otherwise the return value is NULL.

You can use it directly from our toolkit or use it as an example of how to write the function.

You can also see it in action here:

https://youtu.be/beANzSe1-jE

For more information on joining our insiders team to get all our free tools and resources, follow the link here:

http://sdutools.sqldownunder.com

2018-04-04