0

我有一个名为 TimeSpent 的列,类型为 Float。

它包含值(即 2.50, 1.25, 3.75, 5.60 )

我需要将这些小时值转换为适当的小时值,例如 (2:30, 1:15, 3:45 ,5:36) 等)

如何才能做到这一点?

4

3 回答 3

1

试试这个Query

select 
time_decimal
,cast(cast(cast(time_decimal as int) as varchar)+
':'+cast(cast((time_decimal - cast(time_decimal as int)) * 60 as int) as varchar)+
':'+cast(cast(((time_decimal - cast(time_decimal as int)) * 60-
    cast((time_decimal - cast(time_decimal as int)) * 60 as int)) * 60 as int) as varchar) as time) as real_time
from time1

SQL 小提琴

于 2013-05-10T11:12:24.180 回答
0

假设您已经创建了一个数据类型为 time 的列,您可以通过连接 TimeSpent 的左侧直到点和点的右侧部分乘以 60 来更新表。

SQL Server 2012:

UPDATE yourTable SET
newTimeColumn = CONCAT(CAST(TimeSpentFloat AS INT), ':', (TimeSpentFloat - CAST(TimeSpentFloat AS INT)) * 60);

SQL Server 2008 及更低版本:

SELECT 
CAST(CAST(2.75 AS INT) AS CHAR) + ':' + CAST((2.75 - CAST(2.75 AS INT)) * 60 AS CHAR);

看到它在sqlfiddle中工作。

于 2013-05-10T07:58:01.113 回答
0

算一下:例如,通过转换为 int 可以获得的小时数。要获得分钟,请将小数部分乘以 60 等。在单个查询中进行此操作的一种快速而肮脏的方法可能是:

declare @d float
set @d = 1.54
select cast(@d as int) h,
       cast((@d - cast(@d as int)) * 60 as int) m,
       cast(((@d - cast(@d as int)) * 60
            - cast((@d - cast(@d as int)) * 60 as int)) * 60 as int) s
于 2013-05-10T08:05:25.767 回答