的日期仅为日期仅为日期很少,但结果可能是出乎意料的。这是因为DateTime数据类型同时包含日期和时间组件。例如,如果您有一个带有DateTime类型的datecreated列的用户表,则以下查询:从[user] u中选择 *选择 * 其中u.datecreated ='2014-02-07'也不会返回任何记录,即使用户是在2014-02-07在12:30:30:47.220。 u.datecreated> ='2014-02-07'和u.datecreated
This query is SARGable, meaning it can use an index on the DateCreated column.
Select * from [User] U where U.DateCreated = '2014-02-07'Why Not Use Functions?
It may be tempting to use functions like CONVERT to extract the date component from the datetime.但是,不建议这样做。 Using functions in the WHERE clause or join conditions:
Select * from [User] U where U.DateCreated = '2014-02-07'Removes the ability of the optimizer to use an index on the fieldAdds unnecessary calculations for each row of data
Avoiding BETWEEN
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3