比较SQL Server中GETDATE()和Datetime2之间的区别

时间:2014-12-10 16:34:03

标签: sql sql-server getdate

我想检查当前时间(GETDATE())和StartTime之间的小时数差异。

这是我到目前为止所做的:

SELECT 
    CheckedOut, GETDATE() AS CurrentDateTime, StartTime
FROM 
    TimeRecordModels 
WHERE 
    CheckedOut = 0;

如果差异超过15小时,我想让bool CheckedOut为true,然后将列名EndTime设置为GETDATE()

1 个答案:

答案 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;