3 回答
TA贡献1836条经验 获得超3个赞
您要乘以毫秒,因为小数部分将被丢弃。
SELECT DATEADD(ms, 121.25 * 1000, 0)
如果您希望不带日期部分,可以使用样式为114的CONVERT
SELECT CONVERT(varchar, DATEADD(ms, 121.25 * 1000, 0), 114)
TA贡献1789条经验 获得超8个赞
如果您的时间量超过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)
as
begin
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.000
1:36:59.940
35791394:59:56.400
添加回答
举报