Convert Duration seconds to Time format HH:MM:SS in TSQL

In my previous post, I have created a method on crystal report function to convert duration in seconds to Time format. I think it would be useful to have another one for TSQL as well.

Here it is my User Define function to convert duration (Seconds) to Time format.Hours:Minutes:Seconds.

Hope you find it useful.

CREATE FUNCTION [dbo].[ConvertDurationToTime]
(
— Add the parameters for the function here
@Duration INT = 0
)
RETURNS VARCHAR(12)
AS
BEGIN
— Declare the return variable here
DECLARE @ConvertedTime VARCHAR(10)

— Add the T-SQL statements to compute the return value here
DECLARE @HH VARCHAR(5)
DECLARE @MM VARCHAR(3)
DECLARE @SS VARCHAR(3)

IF LEN((@Duration / 3600))
Happy Coding



Leave a Reply