Get records 10 min before system datetime in SQL

2019-01-25 05:22发布

问题:

i need to find the records 10 min before system current datetime.

select Id,TimeStamp from ISAlive where RecordUpdatedDate < GETDATE() --SYSDATETIME()

回答1:

select Id, TimeStamp
from ISAlive
WHERE RecordUpdatedDate = dateadd(minute,-10,getdate())

might be a starting point. Of course, it probably won't match exactly...

...if you want to get the most recent record that fits that criteria, however, try

SELECT TOP 1 ID, TimeStamp
FROM ISAlive
WHERE RecordUpdatedDate <= dateadd(minute, -10, getdate())
ORDER BY RecordUpdatedDate DESC


回答2:

SELECT Id, TimeStamp
FROM ISAlive 
WHERE RecordUpdatedDate < DATEADD(minute,-10, SYSDATETIME());