简体   繁体   English

t-sql获取日期完全匹配或在指定分钟内的记录

[英]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)

Can use DATEADD in this manner, to get the time 5 minutes ago: 可以以这种方式使用DATEADD ,以获取5分钟前的时间:

AND (t.LastAccOnTime = @LastAccOnTime 
     OR t.LastAccOnTime BETWEEN DATEADD(minute, -5, @LastAccOnTime) AND @LastAccOnTime)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM