将小数小时转换为正确的小时数sql

时间:2015-01-07 09:45:50

标签: sql sql-server-2008

我有一个列显示Loggedhours的总和...我有一个这样的记录是52.80,即52小时和80分钟,但这是错误的显示方式。

应该是53.20(53小时20分钟)。我怎么能完成它?

Plz帮助。

1 个答案:

答案 0 :(得分:-1)

如果十进制列的比例为2,则以下解决方案有助于:

declare @val decimal(18,2) =  52.80
declare @vval varchar(30) = cast(@val as varchar(30))
declare @p bigint = charindex('.', @vval, 1)

select 
        case when cast(hhmm.MM as int) >= 60 
                then cast(cast(hhmm.HH as int) + cast(hhmm.MM as int) / 60 as varchar(30)) + ':' + cast(cast(hhmm.MM as int) % 60 as varchar(30))
            else hhmm.HH + ':' + hhmm.MM
        end as T
from
    (select substring(@vval, 0, len(@vval)-@p+1) as HH, substring(@vval, @p+1, len(@vval)-@p+1) as MM) hhmm

如果比例大于2,转换后自动添加的尾随零应单独处理。