Sunday, 25 August 2013

Optimize query selecting based on several date ranges

Optimize query selecting based on several date ranges

It seems like there should be a way to make this more efficient. The
difficulty is the arbitrary date ranges and number of said ranges.
In this query I am attempting to retrieve rows from the tasks table where
the date (regardless of time) is 2013-01-01, 2013-01-03, 2013-01-09 or
2013-02-01
tasks
|id | int |
|begin_date| datetime |
SELECT * FROM tasks
WHERE (tasks.begin_date >= '2013-01-01' AND tasks.begin_date < '2013-01-01')
OR (tasks.begin_date >= '2013-01-03' AND tasks.begin_date < '2013-01-04')
OR (tasks.begin_date >= '2013-01-09' AND tasks.begin_date < '2013-01-10')
OR (tasks.begin_date >= '2013-02-01' AND tasks.begin_date < '2013-02-02')
Is there a "proper" way to do this? or a significantly more efficient way?
I'm using SQL Server 2008.

No comments:

Post a Comment