I'm trying to write a select query where my OrderDate is between two months.
this is my code so far.
declare @FromMonth datetime
declare @ToMonth datetime
set @FromMonth = '20111201'
set @ToMonth = '20120301'
from Order o
where o.OrderDate between @FromMonth and @ToMonth
between rarely works as well as you might hope, I usually find it better to write the range as an inclusive/exclusive pair and apply appropriate comparisons:
declare @FromMonth datetime declare @ToMonth datetime set @FromMonth = '20111201' set @ToMonth = '20120401' select * from Order o where o.OrderDate >= @FromMonth and o.OrderDate < @ToMonth
This will also avoid nastiness if
OrderDate includes a time component.