Querying a Date Range in SQL Server

Since SQL uses datetime as their standard, you can get a wide range of result sets when querying for a date range without the time element. Here are a few examples of different where clauses and their result counts. I have found that using the datediff function with day as the interval is the safest and most effective method of returning ALL the results you are looking for.

Best Method:
where datediff(day, '11/20/2009', so.OpportunityCreateDate) >= 0
and datediff(day, '1/14/2010', so.OpportunityCreateDate) <= 0

Different where clauses and their respective result counts:

where wr.createdt >= '9/09/2009'
and wr.createdt <= '9/30/2009'
--43987

where year(wr.createdt) = 2009 and month(wr.createdt) = 9
and day(wr.createdt) between 9 and 30
--46032

where wr.createdt between '9/9/2009' and '9/30/2009'
--43987

where wr.createdt >= '9/09/2009 12:00:00AM'
and wr.createdt <= '9/30/2009 11:59:59PM'
--46032

Leave a comment