得到四个日期列的差值

时间:2019-02-21 11:40:57

标签: sql sql-server

2个日期列差异查询的代码很好。 但是当我想加入他们的结果时,我得到了错误

CONVERT(VARCHAR(12), DATEADD(MS, DATEDIFF(MS,Stat_EndTime, Stat_StartTime), 0), 108) AS [VisitTime],
CONVERT(VARCHAR(12), DATEADD(MS, DATEDIFF(MS,Load_EndTime, Load_StartTime), 0), 108) AS [LoadTime] 

当我按照以下所示进行连接的完整时差时,我得到了错误

CONVERT(VARCHAR(12), DATEADD(MS, DATEDIFF(MS,Stationary_EndTime, Stationary_StartTime), 0), 108) - CONVERT(VARCHAR(12), DATEADD(MS, DATEDIFF(MS,LoadingEndTime, LoadingStartTime), 0), 108) AS [Actual Contact Time],
  

减法运算符中的数据类型varchar和varchar不兼容。

1 个答案:

答案 0 :(得分:3)

您正在执行减法操作,而不是JOIN。错误非常明显。您正在尝试减去字符串。那是不允许的。

您可以采用两个日期时间之差:

(DATEADD(MS, DATEDIFF(MS, Stationary_EndTime, Stationary_StartTime), 0) -
 DATEADD(MS, DATEDIFF(MS, LoadingEndTime, LoadingStartTime), 0)
) AS Difference