如何以小时和分钟为单位获得两个日期时间之间的差异

2022-01-03 00:00:00 datetime sql sql-server sql-server-2012

如果我有两个这样的日期时间:

If I have two datetimes like this :

transtime_in, transtime_out

如何以下列格式获取这些日期时间之间的差异:

How to get the difference between those datetimes in the following format :

hh:mm

<小时>

我用

DATEDIFF(hour, transtime_in, transtime_out) 

但我只有小时数.

推荐答案

试试这个 -

查询:

DECLARE 
      @transtime_in DATETIME
    , @transtime_out DATETIME

SELECT 
      @transtime_in = '2013-05-19 08:58:07.000'
    , @transtime_out = '2013-05-19 16:40:53.000'

SELECT LEFT(CONVERT(VARCHAR(10), @transtime_out - @transtime_in, 108), 5)

输出:

-----
07:42

相关文章