sql - Optimize query selecting based on several date ranges -
it seems there should way make more efficient. difficulty arbitrary date ranges , number of said ranges.
in query attempting retrieve rows tasks table date (regardless of time) 2013-01-01, 2013-01-03, 2013-01-09 or 2013-02-01
tasks |id | int | |begin_date| datetime | select * tasks (tasks.begin_date >= '2013-01-01' , tasks.begin_date < '2013-01-01') or (tasks.begin_date >= '2013-01-03' , tasks.begin_date < '2013-01-04') or (tasks.begin_date >= '2013-01-09' , tasks.begin_date < '2013-01-10') or (tasks.begin_date >= '2013-02-01' , tasks.begin_date < '2013-02-02')
is there "proper" way this? or more efficient way?
i'm using sql server 2008.
please try this
select * tasks convert(varchar,begin_date,103) in ('01/01/2013','04/01/2013','10/01/2013','02/02/2013')
or can try also.
select * tasks (convert(varchar,begin_date,103) ='01/01/2013' or convert(varchar,begin_date,103) = '04/01/2013' or convert(varchar,begin_date,103) = '10/01/2013' or convert(varchar,begin_date,103) = '02/02/2013')
or third way
select * tasks (tasks.begin_date between '2013-01-01' , '2013-01-01') or (tasks.begin_date between '2013-01-03' , '2013-01-04') or (tasks.begin_date between '2013-01-09' , '2013-01-10') or (tasks.begin_date between '2013-02-01' , '2013-02-02')
Comments
Post a Comment