获取日期时间列 = 今天的行 - SQL 服务器菜鸟 [英] Get row where datetime column = today - SQL server noob

查看:24
本文介绍了获取日期时间列 = 今天的行 - SQL 服务器菜鸟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 sql 2005 中,不是从 dateparts 年、月和日期构建查询,

In sql 2005, instead of building a query from dateparts year, month and date,

有没有更简洁的方式来编写 where 子句?

is there an more succinct way of writing the where clause?

推荐答案

在 SQL 2000 和 SQL 2005 中,您可以使用漂亮的 select 语句从 DateTime 中删除时间组件,即

In SQL 2000 and SQL 2005 you can use a nice select statement to remove the time component from a DateTime, ie

SELECT DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0)

将于 2010 年 4 月 6 日返回(仅限今天).

will return 6-Apr-2010 (well for today only).

所以结合 marc_s 的回答,你会想要

So combined with marc_s's answer, you'd want

SELECT (list of fields)
FROM dbo.YourTable
WHERE dateValue BETWEEN DATEADD(dd, DATEDIFF(dd,0,'MY Date and Time, But I Only Want Date'), 0) 
AND DATEADD(dd, DATEDIFF(dd,0,'MY Date and Time, But I Only Want Date'), 1)

已更改以适应要求,请注意第二个 DateAdd 中的 1(这会将它从开始的天数添加到 1(而不是 0),使其成为 2010 年 4 月 7 日 00:00:00)如果你想要 6th Apr 23:59:59 你离第二个日期还有一秒钟

Changed to suit requirements, note the 1 in the second DateAdd (this adds the days it has been from the beginning to 1 (instead of 0), making it 7th Apr 2010 00:00:00) If you want 6th Apr 23:59:59 you take a second away from the second date

DATEADD(ss,-1,'My DateTime')

最终电话将成为

DATEADD(ss,-1,DATEADD(dd, DATEDIFF(dd,0,'MY Date and Time, But I Only Want Date'), 1))

好的,这一次提供了很多信息!希望一切都有意义:)

Ok that's alot of info at once! Hope it all makes sense :)

这篇关于获取日期时间列 = 今天的行 - SQL 服务器菜鸟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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