Technical Article

UDF Function to convert seconds to HH:MM:SS format

,

SFormat is UDF function that can convert
seconds to HH:MM:SS
Ex:
1- Select dbo.SFormat(130) will return 00:02:10
2- Select dbo.SFormat(dbo.test.Duration) as Dur (to be used with tables). where 'Test' is table name and 'Duration is a field(int).

CREATE function dbo.SFormat(@SecTime as int)
RETURNS Varchar(10)
As
Begin
Declare @Hour as Varchar(4)
Declare @Min as Varchar(2)
Declare @Sec as varchar(2)
Declare @RetTime as varchar(10)

Select @Hour=Round(@SecTime/3600,0,1)
If Len(@Hour)=1 
  BEGIN
 select @Hour = '0'+ @Hour   
End
 
Select @Min=Round((@SecTime % 3600)/60 ,0,1)
If Len(@Min)=1 
  BEGIN
 select @Min='0'+ @Min 
End

Select @Sec = (@SecTime % 3600)%60
If Len(@Sec)=1 
  BEGIN
  Select @Sec='0'+ @Sec 
End

set @RetTime=@Hour + ':' + @Min + ':' + @Sec

Return (@RetTime)
end

Rate

5 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

5 (1)

You rated this post out of 5. Change rating