sql将字符串hh:mm:ss转换为十进制小时

uxh89sit  于 2021-05-27  发布在  Hadoop
关注(0)|答案(1)|浏览(390)

我在一个表中有一列lvl\u tim,它是返回hh:mm:ss的字符串类型。我不能采用datefiff()方法,最终希望对这些值求和。
我尝试过:

cast(lvl_tim as int)

我想要的结果是0:10:10,返回0.1683小时。

ktecyv1j

ktecyv1j1#

你可以采用暴力算法:

WITH yourTable AS (
    SELECT '12:34:56' AS time
)

SELECT
    time,
    CAST(LEFT(time, 2) AS int) +
    CAST(SUBSTRING(time, 4, 2) AS int) / 60.0 +
    CAST(SUBSTRING(time, 7, 2) AS int) / (60.0*60.0) AS decimal_hours
FROM yourTable;

演示

上述查询的正确输出是 12.58222155 十进制小时,输入时间为 12:34:56 .

相关问题