[英]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. 我正在寻找一条记录,其中日期是完全匹配或在5分钟内。
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
当我传递
@LastAccOnTime
我还要匹配一条记录,该记录距当前最大t.LastAccOnTime
最多5分钟。
You could use the datediff
function to check the difference between t.LastAccOnTime
and @LastAccOnTime
: 您可以使用
datediff
函数检查t.LastAccOnTime
和@LastAccOnTime
之间的差异:
SELECT @TripId = t.Id
FROM Obd.TodaysTrip t WITH(NOLOCK)
WHERE (t.DeviceId = @Id) AND
(ABS(DATEDIFF(MINUTE, t.LastAccOnTime, @LastAccOnTime)) <= 5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.