我的问题是,
我在 SQL 表中有一个 DateTime 类型的列,它包含如下值
2013-02-01 10:00:00.000
2013-01-27 16:00:00.000
2013-02-01 14:00:00.000
如果我只在 Where 子句中提及日期,我如何通过查询获得结果,例如:
SELECT *
FROM tablename
WHERE columnName = '2013-02-01'
执行这个查询后我想得到这个结果
2013-02-01 10:00:00.000
2013-02-01 14:00:00.000
请帮助我。
请您参考如下方法:
将函数应用于该列将使您的查询成为 non-sargable它会弄乱你的Cardinality Estimation .
改用间隔。
select *
from tablename
where columnName >= '20130201' and
columnName < '20130202'
备注:The dateformat YYYY-MM-DD is not always interpreted the way you think in SQL Server .
为避免日期解释的歧义,请使用通用日期格式文字“YYYYMMDD”。
set language German
go
select convert(datetime, '1989-06-12'),
convert(datetime, '19890612'),
convert(datetime2, '1989-06-12'),
convert(datetime2, '19890612'),
convert(date, '1989-06-12'),
convert(date, '19890612')




