虽然在T-SQL中将时间转换为十进制输出会有所不同?

时间:2018-10-21 07:30:17

标签: sql sql-server

我在下面的查询中尝试将时间转换为十进制,但输出有所不同

SELECT
    in_time, out_time,  
    DATEDIFF(second, in_time, out_time) / 3600.0,
    TotalHrs 
FROM
    table

结果:

    in_time                       out_time              (Vary output)      TotalHrs
    2018-10-19 07:18:54.000     2018-10-19 19:03:06.000     11.736666          11:44:12.0000000

预期输出:

11:44 as decimal

我想用一些数字来计算时间

 11:44 * 50

2 个答案:

答案 0 :(得分:1)

使用此:

 cast (RTRIM(DATEDIFF(second, in_time, out_time)/3600)+'.'
+ RIGHT('0'+RTRIM((DATEDIFF(second, in_time, out_time) % 3600)/60),2)as numeric(18,2))

你也可以用这个来计算

 cast (RTRIM(DATEDIFF(second, in_time, out_time)/3600)+'.'
    + RIGHT('0'+RTRIM((DATEDIFF(second, in_time, out_time) % 3600)/60),2)as numeric(18,2)) * 50

输出:

  

11.44

答案 1 :(得分:0)

MySQL (MySQL中的问题originally tagged)中,您可以使用TimeDiff()函数来获取两个MySQL Datetime format值之间的差异('YYYY-MM-DD HH:MM:SS' ),并获得 MySQL日期时间格式的输出

尝试:

select in_time,
       out_time, 
       TIMEDIFF(out_time, in_time),
       TotalHrs 
from table

演示:

SELECT timediff('2018-10-19 19:03:06.000','2018-10-19 07:18:54.000');

+---------------------------------------------------------------+
| timediff('2018-10-19 19:03:06.000','2018-10-19 07:18:54.000') |
+---------------------------------------------------------------+
| 11:44:12.000                                                  |
+---------------------------------------------------------------+
1 row in set (0.00 sec)