延续上篇随笔,直接放代码了,调用是一样的,现在修改下自定义函数:
View Code
1 ------------------------------------------------------ 2 -- Usage: Convert seconds to Hour:Minute:Second 3 ------------------------------------------------------ 4 create function f_SecondsToHourMinuteSecond(@seconds bigint) 5 returns varchar(20) 6 as 7 begin 8 declare @str as varchar(20) 9 if @seconds >= 10 --如果秒数大于等于10 10 begin 11 declare @day as varchar(10) 12 declare @hour as varchar(10) 13 declare @minute as varchar(10) 14 declare @second as varchar(10) 15 16 if(@seconds/86400<10) 17 begin 18 set @day='0'+convert(varchar(10),@seconds/86400) 19 end 20 else 21 begin 22 set @day=convert(varchar(10),@seconds/84600) 23 end 24 25 if(@seconds%86400/3600 < 10) 26 begin 27 set @hour = '0' + convert(varchar(10),@seconds%86400/3600) 28 end 29 else 30 begin 31 set @hour = convert(varchar(10),@seconds%86400/3600) 32 end 33 34 if(@seconds%86400%3600/60 < 10) 35 begin 36 set @minute = '0' + convert(varchar(10),@seconds%86400%3600/60) 37 end 38 else 39 begin 40 set @minute = convert(varchar(10),@seconds%86400%3600/60) 41 end 42 43 if(@seconds%86400%3600%60 < 10) 44 begin 45 set @second = '0' + convert(varchar(10),@seconds%86400%3600%60%60) 46 end 47 else 48 begin 49 set @second = convert(varchar(10),@seconds%86400%3600%60%60) 50 end 51 set @str = @day+'天'+@hour + '小时' + @minute + '分钟' + @second +'秒' 52 end 53 else --如果秒数小于10 54 begin 55 set @str = '00:00:0'+convert(varchar(20),@seconds) 56 end 57 return(@str) 58 end
查询结果如下: