Sql server 操作数数据类型时间对于减法运算符无效

Sql server 操作数数据类型时间对于减法运算符无效,sql-server,Sql Server,我遇到了一个问题,这个代码对我不起作用。是的,据我所知,一切都是正确的,但我不断收到一个错误的线路。我知道这是其他问题的重复,我已经搜索过了,但没有任何效果 convert(time(0),(isnull(solver_endtime,'')-isnull(solver_starttime,''))) as solverruntime, 这是完整的代码,以防我遗漏了什么 IF OBJECT_ID('staging.dbo.Log_Batch_Report_2', 'U') IS NOT NUL

我遇到了一个问题,这个代码对我不起作用。是的,据我所知,一切都是正确的,但我不断收到一个错误的线路。我知道这是其他问题的重复,我已经搜索过了,但没有任何效果

convert(time(0),(isnull(solver_endtime,'')-isnull(solver_starttime,''))) as solverruntime,
这是完整的代码,以防我遗漏了什么

IF OBJECT_ID('staging.dbo.Log_Batch_Report_2', 'U') IS NOT NULL
merge into staging.dbo.log_batch_report_2 a
using 
(select batch,
    starttime,
    endtime,
    convert(time(0),(isnull(endtime,'')-isnull(starttime,''))) as totalruntime,
    convert(time(0),(isnull(solver_endtime,'')-isnull(solver_starttime,''))) as solverruntime,
    convert(time(0),((isnull(endtime,'')-isnull(starttime,''))- (isnull(solver_endtime,'')-isnull(solver_starttime,'')))) as non_solverruntime,
    to_time
from staging.dbo.log_batch_report
) b
on a.batch=b.batch and a.starttime=b.starttime and a.endtime=b.endtime
when matched then update set a.batch=a.batch
when not matched then insert (batch,batchdate,logility_up_time,starttime,endtime,totalruntime,solverruntime)
values (b.batch,b.starttime,b.endtime,b.starttime,b.endtime,b.totalruntime,b.solverruntime);
编辑

这是我从代码中的totalruntime行中得到的。那条线和另外两条线有什么区别


要获得两个日期之间的差异,可以使用以下方法:

SELECT starttime, endtime, 
    Days = DATEDIFF(second, starttime, endtime) / 86400,
    Hours = DATEDIFF(second, starttime, endtime) % 86400 / 3600,
    Minutes = DATEDIFF(second, starttime, endtime) % 3600 / 60,
    Seconds = DATEDIFF(second, starttime, endtime) % 60

我确信DATEDIFF是有效的,但我学到的是,我更大的问题是STARTDATE晚于ENDDATE的数据错误,提供了否定的答案。因此,我需要在其中添加一个子句,以便只在ENDDATE>STARTDATE时进行计算

IF OBJECT_ID('staging.dbo.Log_Batch_Report_2', 'U') IS NOT NULL
merge into staging.dbo.log_batch_report_2 a
using 
(select batch,
    starttime,
    endtime,
    solver_starttime,
    solver_endtime,
    CASE WHEN endtime>starttime THEN convert(time(0),(isnull(endtime,''))-(isnull(starttime,''))) 
        ELSE null
    END as totalruntime,
    CASE WHEN solver_endtime>solver_starttime THEN convert(time(0),(isnull(solver_endtime,''))-(isnull(solver_starttime,''))) 
        ELSE null
    END as solverruntime,
    CASE WHEN endtime>starttime and solver_endtime>solver_starttime and (endtime-starttime)>(solver_endtime-solver_starttime) 
            THEN convert(time(0),((isnull(endtime,'')-isnull(starttime,''))-((isnull(solver_endtime,'')-isnull(solver_starttime,''))))) 
        ELSE null
    END as non_solverruntime,
    to_time
from staging.dbo.log_batch_report
) b
on a.batch=b.batch and a.starttime=b.starttime and a.endtime=b.endtime
when matched then update set a.batch=a.batch
when not matched then insert (batch,batchdate,logility_up_time,solver_starttime,solver_endtime,starttime,endtime,totalruntime,solverruntime)
values (b.batch,b.starttime,b.endtime,b.solver_starttime,b.solver_endtime,b.starttime,b.endtime,b.totalruntime,b.solverruntime);

这在技术上可能不正确,但到目前为止,它产生了我正在寻找的以小时:分钟:秒为单位的格式的差异,即使时间在不同的日子

不要使用基本减法。而是使用DATEADD。所以我可以使用datediff和dateadd一样,对吗?此外,dateadd似乎只允许一个日期。我怎样才能得到完全的差异(小时、分钟、秒)?如果你有秒数,那么得到部分只是数学问题。除以3600得到小时,除以60得到分钟。然后用所有的秒%60得到剩余的。记住,时间不是时间跨度,而是时间点。所以减去它们有点奇怪。你希望下午3点到10点的结果是什么?我希望它告诉我5个小时(00:05:00),就像totalruntime片段(上面一行)那样。但是datediff主要起作用。现在,我有一个问题,从数据类型int转换为time,这是不允许的。将整数转换为时间有点困难。13点是几点?214怎么样?