I have a SQL datetime
field in a very large table. It's indexed and needs to be queried.
The problem is that SQL always stores the time component (even though it's always midnight), but the searches are to the day, rather than time.
declare @dateVar datetime = '2013-03-11;
select t.[DateColumn]
from MyTable t
where t.[DateColumn] = dateVar;
Won't return anything, as the t.[DateColumn]
always includes a time component.
My question is what is the best way round this?
There seem to be two main groups of options:
Create a second variable using
dateadd
and use abetween ... and
or>= ... and ... <=
.Convert the
t.[DateColumn]
into a date-only component - I think this will cause any indexes to be ignored.
Both of these seem very messy - I don't really want to be making a range comparison or scan the table.
Is there a better way?
If one of these options is consistently optimal way then how and why?