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不兼容。
答案 0 :(得分:3)
您正在执行减法操作,而不是JOIN
。错误非常明显。您正在尝试减去字符串。那是不允许的。
您可以采用两个日期时间之差:
(DATEADD(MS, DATEDIFF(MS, Stationary_EndTime, Stationary_StartTime), 0) -
DATEADD(MS, DATEDIFF(MS, LoadingEndTime, LoadingStartTime), 0)
) AS Difference