我想检查当前时间(GETDATE()
)和StartTime
之间的小时数差异。
这是我到目前为止所做的:
SELECT
CheckedOut, GETDATE() AS CurrentDateTime, StartTime
FROM
TimeRecordModels
WHERE
CheckedOut = 0;
如果差异超过15小时,我想让bool CheckedOut
为true,然后将列名EndTime
设置为GETDATE()
答案 0 :(得分:1)
使用DATEDIFF功能。
SELECT CheckedOut, GETDATE() AS CurrentDateTime, StartTime,
DATEDIFF(hour, StartTime, GETDATE()) AS HoursDifference
FROM TimeRecordModels
WHERE CheckedOut = 0;
完成更新:
UPDATE TimeRecordModels
SET CheckedOut = 1,
EndTime = GETDATE()
WHERE DATEDIFF(hour, StartTime, GETDATE()) > 15
AND CheckedOut = 0;