按日期时间字段选择最新记录 [英] Select latest records by datetime field

查看:32
本文介绍了按日期时间字段选择最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何按 SQL Server 的日期时间选择最新记录?

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

这是伪代码...

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

假设当前时间是晚上 10:25:39

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

SQL 查询应该返回这些记录...

the SQL query should return these records...

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

推荐答案

真实代码:

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

这篇关于按日期时间字段选择最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆