无法获得所需格式的 2 个日期之间的差异

Unable to get difference between 2 dates in required format

我的开始和结束列的格式是 Yyyy-mm-dd HH:mm:ss.ms 我想要两者之间的区别是 HH:mm:ss.ms 格式。我该怎么做?

我的查询如下所示:

select *, convert(time, 
        Dateadd(s, 
            Datediff(s, A.Finish, A.Start), 
            Cast('1900-01-01 00:00:00.000000' as datetime2)
        )
    ) as dif 
from (
    select *,
        dateadd(s,convert(int,left(start,10)),'1970-01-01') as Start,
       dateadd(s,convert(int,left(finish,10)),'1970-01-01') as Finish,     
    from tableB
) A 
order by dif asc

我已经在内部查询中将 unix 时间戳转换为标准格式。当我 运行 这个开始日期和开始时间显示为 '2019-12-11 15:45:20.000' 和 '2019-12-12 15:45:17.000' 但我的 dif 显示为'00:00:03',这是错误的。

如有任何帮助,我们将不胜感激

我使用了两个资源来帮助你:

https://www.rodolforodarte.com/2011/05/using-datediff-to-display-hhmmss/

Show datediff as seconds, milliseconds

这是我的结果代码:

DECLARE @START_DATE DATETIME
DECLARE @END_DATE   DATETIME
SET     @START_DATE = '2011-01-01 16:00:22.000'
SET     @END_DATE =   '2011-01-01 22:47:21.022'

SELECT    CONVERT(varchar(6), DATEDIFF(MILLISECOND, @START_DATE, @END_DATE)/3600000)
+ ':'
+ RIGHT('0' + CONVERT(varchar(2), (DATEDIFF(MILLISECOND, @START_DATE, @END_DATE) % 3600000) / 60000), 2)
+ ':'
+ RIGHT('0' + CONVERT(varchar(2),   ((DATEDIFF(MILLISECOND, @START_DATE, @END_DATE) % 3600000) % 60000) / 1000), 2)
+ ':'
+ RIGHT('000' + CONVERT(varchar(2), (((DATEDIFF(MILLISECOND, @START_DATE, @END_DATE) % 3600000) % 60000) % 1000)), 3) AS 'HH:MM:SS:MS'

And here is a small demo