我的时间间隔格式为' 88:52:57' 我需要将其转换为格式为88.88的十进制小时
我该怎么做?
我最初将数据加载为varchar
答案 0 :(得分:4)
您可以使用left,right和substring来提取值,然后进行一些计算。
declare @S varchar(8) = '88:52:57';
select left(@S, 2) + substring(@S, 4, 2)/60.0 + right(@S, 2)/60.0/60.0;
如果您不总是有两位数值,则可以使用parsename来获取值。
declare @S varchar(20) = '088:052:057';
select parsename(S.X, 3) + parsename(S.X, 2)/60.0 + parsename(S.X, 1)/60.0/60.0
from (select replace(@S, ':', '.')) as S(X)
答案 1 :(得分:1)
尝试这样(最好从此创建一个UDF):
首先,我通过XML将时间变量分割为双点。其余的是简单的计算...
DECLARE @YourTime VARCHAR(100)='88:52:57';
WITH Splitted AS
(
SELECT CAST('<x>' + REPLACE(@YourTime,':','</x><x>') + '</x>' AS XML) TimeParts
)
,TimeFract AS
(
SELECT TimeParts.value('/x[1]','float') AS HourPart
,CAST(TimeParts.value('/x[2]','int') * 60 + TimeParts.value('/x[3]','int') AS FLOAT) Seconds
FROM Splitted
)
SELECT HourPart + Seconds/3600
FROM TimeFract
结果
88,8825
答案 2 :(得分:1)
尝试这个,解决方案基于转换,如果格式始终是(h)h:mi:ss:
,使其安全DECLARE @S varchar(8) = '88:52:57';
SELECT
CAST(REPLACE(left(@S, 2), ':', '') as int)+
CAST(CAST(CAST('0:'+RIGHT(@S, 5) as datetime) as decimal(10,10)) * 24 as decimal(2,2))
结果:
88.88