Wednesday, July 1, 2009

SQL: UDF to convert the seconds into HH:MM:SS

-- =============================================
-- Author: Sabah u din Irfan
-- Description: A UDF to convert the seconds into HH:MM:SS
-- Example: SELECT fn_SEC2HHMMSS(65)
-- =============================================
CREATE FUNCTION [dbo].[fn_SEC2HHMMSS]( @sec as int )
RETURNS VARCHAR (15)
AS
BEGIN


RETURN
case len(convert(varchar(15),@sec/3600))
when 1
then RIGHT('00'+convert(varchar(5),@sec/3600),2)
else convert(varchar(15),@sec/3600)
end
+':'+RIGHT('0'+convert(varchar(5),@sec%3600/60),2)
+':'+RIGHT('0'+convert(varchar(5),(@sec%60)),2)


END

No comments: