Technical Article

Convert seconds to a timestring

,

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)

 

 

create function [dbo].[udf_seconds2text](@duration int) returns varchar(8)
as
    begin
        return (select CONVERT(varchar(8),DATEADD(second,@duration,0),114))
    end
go

Rate

4.2 (5)

You rated this post out of 5. Change rating

Share

Share

Rate

4.2 (5)

You rated this post out of 5. Change rating