Jesse Jesse -4 years ago 69
SQL Question

t-sql get record where date is exact match or within specified minutes

I'm looking to get a record where the date is either an exact match or within 5 minutes.

SELECT @TripId = t.Id
FROM Obd.TodaysTrip t WITH(NOLOCK)
WHERE (t.DeviceId = @Id)
AND (t.LastAccOnTime = @LastAccOnTime)


When I pass @LastAccOnTime I would like to also match on one single record that is at the most 5 minutes prior to the current greatest t.LastAccOnTime

Answer Source

Can use DATEADD in this manner, to get the time 5 minutes ago:

AND (t.LastAccOnTime = @LastAccOnTime 
     OR t.LastAccOnTime BETWEEN DATEADD(minute, -5, @LastAccOnTime) AND @LastAccOnTime)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download