在最后15分钟的日期和时间中选择两列中的更改记录

时间:2017-02-13 18:53:13

标签: sql-server tsql select

我需要选择过去15分钟内更改的记录,这会有效吗?

--Select changed records
USE PC

SELECT        
    PEOPLE_CODE, PEOPLE_ID, PEOPLE_CODE_ID, PREVIOUS_ID, 
    FIRST_NAME, MIDDLE_NAME, LAST_NAME, REVISION_DATE, REVISION_TIME
FROM
    PEOPLE
WHERE 
    REVISION_DATE >= getdate()
    --Also need to check for time
    AND REVISION_TIME >=  dateadd(minute,-15,getdate()) --Test record
ORDER BY 
    REVISION_DATE DESC, REVISION_TIME DESC

1 个答案:

答案 0 :(得分:0)

我认为你就在那里,只需要组合日期和时间列值。您可以使用隐式或显式方法。隐含的优点是它快速且易于编写。显式选项的优点是,如果REVISION_DATE列的时间部分填充了00:00:00.000之外的其他内容,或者REVISION_TIME的日期部分填充了除1900-01-01以外的其他内容USE PC SELECT PEOPLE_CODE, PEOPLE_ID, PEOPLE_CODE_ID, PREVIOUS_ID, FIRST_NAME, MIDDLE_NAME, LAST_NAME, REVISION_DATE, REVISION_TIME FROM PEOPLE WHERE (REVISION_DATE + REVISION_TIME) >= dateadd(minute,-15,getdate()) ORDER BY REVISION_DATE DESC, REVISION_TIME DESC ,它仍然会按预期工作。

<强>隐式:

USE PC

SELECT        
    PEOPLE_CODE, PEOPLE_ID, PEOPLE_CODE_ID, PREVIOUS_ID, 
    FIRST_NAME, MIDDLE_NAME, LAST_NAME, REVISION_DATE, REVISION_TIME
FROM
    PEOPLE
WHERE 
    (dateadd(d, datediff(d, 0, REVISION_DATE), 0) + dateadd(d, -datediff(d, 0, REVISION_TIME), REVISION_TIME)) >=  dateadd(minute,-15,getdate())
ORDER BY 
    REVISION_DATE DESC, REVISION_TIME DESC

<强>显

libc++abi.dylib: terminating with uncaught exception of type NSException