我在一个非常大的表中有一个sql datetime字段。它被索引,需要查询。
@H_301_21@问题是sql总是存储时间组件(即使它总是午夜),但搜索是一天,而不是时间。
declare @dateVar datetime = '2013-03-11; select t.[DateColumn] from MyTable t where t.[DateColumn] = dateVar;
不会返回任何东西,因为t。[DateColumn]总是包含时间分量。
我的问题是这是最好的方法?
似乎有两个主要的选择:
>使用dateadd创建第二个变量,并在…之间使用,或者> = …和…< =。
>将t。[DateColumn]转换为只有日期的组件 – 我认为这将导致任何索引被忽略。
这两个都看起来很混乱 – 我真的不想做一个范围比较或扫描表。
有没有更好的办法?
如果这些选项之一是一贯的最佳方式,那么如何和为什么?