的日期僅為日期僅為日期很少,但結果可能是出乎意料的。這是因為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