开发者

Select most recent rows based on a DateTime field within 5 mins of current time

开发者 https://www.devze.com 2023-01-21 13:54 出处:网络
In a SQL Server table, I have a DateTime field and every time I have a login failure for an external web service I write the current DateTime to the table.

In a SQL Server table, I have a DateTime field and every time I have a login failure for an external web service I write the current DateTime to the table.

I currently have code that parses through the returns and determines if there are > 2 failures within 5 mins. If so I have a flag to turn off calling of the web service. I am curious if there is a way I can just use sql to return the number of rows that are within 5 mins from th开发者_如何学JAVAe current time?


This will return all records that are less than five minutes old:

SELECT [Columns]
FROM [YourTable]
WHERE [DateTimeField] >= dateadd(minute, -5, getdate())

You can get more information on DATEADD here .


You can try this

SELECT COUNT(*)
FROM MyTable
WHERE MyDateTimeField > DATEADD(mm, -5, GETDATE())
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号