sql日期+时间+时间差异计算

时间:2017-10-26 13:42:36

标签: sql datetime

说我有三列,depart_date,depart_time和depart_diff(表示真正的depart_time - 计划时间,单位为分钟),我怎样才能真正使用sql。

例如,我有离开日期:2017-05-05;                    出发时间:00:20:00;                    depart_diff:-30(意思是早30分钟)

我应该得到真实的离开日期:2017-05-04;              real depart_time:23:50:00

由于

1 个答案:

答案 0 :(得分:0)

您应该阅读DATEADD()

在你的情况下,这应该是:

declare @depart_date as date = '2017-05-05'
declare @depart_time as time = '00:20:00'
declare @depart_diff as int = -30


SELECT DATEADD(MINUTE ,@depart_diff, CAST(@depart_date AS DATETIME)+CAST(@depart_time AS DATETIME))

第三个参数是@depart_date@depart_time连接为datetime