Convert SQL server datetime fields to compare date parts only, with indexed lookups

前端 未结 5 774
野性不改
野性不改 2020-11-30 00:23

I\'ve been doing a convert(varchar,datefield,112) on each date field that I\'m using in \'between\' queries in SQL server to ensure that I\'m only accounting fo

5条回答
  •  情话喂你
    2020-11-30 00:28

    It's correct - doing the conversion will execute the conversion for every row queried. It's better to leave the date columns as dates, and pass in your where clauses as dates:

    select * from appointments where appointmentdate between 
    '08/01/2008' AND '08/16/2008'
    

    Note: Leaving off the time means midnight (00:00.000), so you will include all times for 08/01, and all times from 08/15, and anything that is exactly 08/16/2008 00:00:00

提交回复
热议问题