开发者

Select latest records by datetime field

开发者 https://www.devze.com 2022-12-09 06:08 出处:网络
How could i select the latest records by datetime of an SQL Server? Here is the pseudo-code... SELECT Records

How could i select the latest records by datetime of an SQL Server?

Here is the pseudo-code...

SELECT Records 
  FROM MyTable 
 WHERE current time >= (CurrentTime - 2 minutes)

Supposing the current Time is 10:25:39 pm

26/10/2009 10:25:39 pm
26/10/2009 10:25:00 pm
26/10/2009 10:24:53 pm
26/10/2009 10:24:19 pm
26/10/2009 10:23:58 pm
26/10/2009 10:14:56 pm
26/10/2009 10:12:56 pm

the SQL query should return these records...

26/10/20开发者_开发技巧09 10:25:39 pm
26/10/2009 10:25:00 pm
26/10/2009 10:24:53 pm
26/10/2009 10:24:19 pm


Real code:

SELECT * FROM MyTable WHERE currentTime >= DATEADD(n, -2,  GETDATE())
ORDER BY currentTime DESC


Use:

WHERE t.currenttime BETWEEN DATEADD(mi, -2, GETDATE()) AND GETDATE()
ORDER BY t.currenttime DESC

References:

  • DATEADD
  • BETWEEN
0

精彩评论

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