拉丁的传说
如果您的时间量超过24小时,则DATEADD和CONVERT方法将无法正确处理。SELECT CONVERT(varchar, DATEADD(ms, 24*60*60 * 1000, 0), 114)00:00:00:000以下功能将处理超过24小时的时间(最大35,791,394小时)。create function [dbo].[ConvertTimeToHHMMSS]( @time decimal(28,3), @unit varchar(20))returns varchar(20)asbegin declare @seconds decimal(18,3), @minutes int, @hours int; if(@unit = 'hour' or @unit = 'hh' ) set @seconds = @time * 60 * 60; else if(@unit = 'minute' or @unit = 'mi' or @unit = 'n') set @seconds = @time * 60; else if(@unit = 'second' or @unit = 'ss' or @unit = 's') set @seconds = @time; else set @seconds = 0; -- unknown time units set @hours = convert(int, @seconds /60 / 60); set @minutes = convert(int, (@seconds / 60) - (@hours * 60 )); set @seconds = @seconds % 60; return convert(varchar(9), convert(int, @hours)) + ':' + right('00' + convert(varchar(2), convert(int, @minutes)), 2) + ':' + right('00' + convert(varchar(6), @seconds), 6)end用法:select dbo.ConvertTimeToHHMMSS(123, 's')select dbo.ConvertTimeToHHMMSS(96.999, 'mi')select dbo.ConvertTimeToHHMMSS(35791394.999, 'hh')0:02:03.0001:36:59.94035791394:59:56.400