mardi 21 avril 2015

Query to check if record has existed for more than X hours

Here's a sample data.

ID | Date
---------
1  | 4/21/2015 11:00:00 AM
1  | 4/21/2015 01:00:00 PM

Let's say it's currently 2 PM, I need to query ID number 1 only if the time difference between the Column Date and Now is >= 2 hours.

Select ID from Table where datediff(hour, Date, getdate()) >= 2 and ID = '1'

Now this query would return the 1st record with the 11 AM time, but I want to ignore other records and just check if the latest record has existed for 2 or more hours. How should I change my query so that I will not get any results if the current time is 2 PM and my last record is 1 PM.

Aucun commentaire:

Enregistrer un commentaire