0

my table's schema is something like

Table A
Id | val | updatedtime

Where updatedtime column is of the type Datetime,

I need to find all the records from db where the time difference between current time and updatedtime of the db is less than 5 min. I couldn't find any right approach on the internet

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
rampuriyaaa
  • 4,926
  • 10
  • 34
  • 41
  • I've changed the title because SQL Server has an unfortunately named (though thankfully the name is deprecated) type called `timestamp` that has nothing to do with dates and times. – Damien_The_Unbeliever Jan 17 '14 at 10:08

3 Answers3

4

Like such?

SELECT *
FROM TableA
WHERE updatetime > DATEADD(MI, -5, GETDATE())
MarkD
  • 5,276
  • 1
  • 14
  • 22
0
SELECT *
FROM TableA
WHERE DATEDIFF(minute,updatedtime,GETDATE()) < 5

Yo can use DATEDIFF

Returns the count (signed integer) of the specified datepart boundaries crossed between the specified startdate and enddate

Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
  • Well in this case this kind of error is expected.."Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. " – rampuriyaaa Jan 17 '14 at 10:14
  • Your query will negate indexes on updatedtime. SARG. – MarkD Jan 17 '14 at 10:16
0

Try the below SQL

SELECT *
FROM YourTable
WHERE (DateDiff(mi, updatetime, GETDATE())) < 5
Naveen
  • 1,496
  • 1
  • 15
  • 24