SQL Server Date Range Filter

Can someone give me the SQL needed to filter by a date range in a "where" clause?

I am looking for something like this:

select *
from tableName
where EffectiveDate between <<a>> and <<b>>

In this example, I want <<a>> to be the first day of the month 6 months prior to the current date (today this would be 4/1/2011) and I want <<b>> to be the last day of the month 6 months in the future (today this would be 3/31/2012).

<<a>> and <<b>> should be 1 full year apart (i.e. 4/1/2011 - 3/31/2012).
jbaird123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vincem1099Commented:
Try this code
select *
from tableName
where EffectiveDate between convert(date,convert(varchar,month(DATEADD(month,-6,GETDATE())))+'/1/'+convert(varchar,year(DATEADD(month,-6,GETDATE())))) and convert(date,convert(varchar,month(DATEADD(month,6,GETDATE())))+'/1/'+convert(varchar,year(DATEADD(month,6,GETDATE()))))

Open in new window

0
jbaird123Author Commented:
vincem1099:

I tried it and there are two problems:

1. I get a message saying that "date" is not a valid type, so I changed this to "datetime".  
2. The second date returned is actually 4/1/2012, but I need this to be 1 day prior (i.e. 3/31/2012).
0
vincem1099Commented:
Ok, I changed first date to calculate one day prior
select *
from tableName
where EffectiveDate between select dateadd(day,-1,convert(datetime,convert(varchar,month(DATEADD(month,-6,GETDATE())))+'/1/'+convert(varchar,year(DATEADD(month,-6,GETDATE()))))) and convert(datetime,convert(varchar,month(DATEADD(month,6,GETDATE())))+'/1/'+convert(varchar,year(DATEADD(month,6,GETDATE()))))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Patrick MatthewsCommented:
Safest way:

select *
from tableName
where EffectiveDate >= DATEADD(month, DATEDIFF(month, 0, GETDATE()) - 6, 0) AND
    EffectiveDate < DATEADD(month, DATEDIFF(month, 0, GETDATE()) + 6, 0)

Open in new window

0
jbaird123Author Commented:
thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.