Search the data of actual month and the two previous months

hello ,
I need to search the data related the actual month and the two previous months
I use the below SQL request:
WHERE datepart(YYYY,dTime)=
datepart(YYYY,GETDATE( )) and datepart(m,dTime) in (datepart(m,GETDATE( )),datepart(m,(dateadd(m,-1,GETDATE( )))),datepart(m,(dateadd(m,-2,GETDATE( )))));
this request work fine , but when I’m in January i don’t have the data .. and this normal
because I use datepart(YYYY,GETDATE( ))  … and I don’t found the good request ..
can someone help me plz.
Thanks
mell lian90Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Vaibhav GoelMSBI , SQL ConsultantCommented:
Hello Mell Lian90
I need to search the data related the actual month and the two previous months

WHERE
      dTime >= DATEADD(MONTH,-2,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))
      AND dTime < SELECT DATEADD(MONTH,1,DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0))
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
Lee SavidgeCommented:
Are you looking to find records where the date is between NOW and last month and month prior to that?

So for today you'd want all records for December, November, and October?

The field that holds the date in question is dTime?
0
Lee SavidgeCommented:
If so, this works:

declare @d datetime

select @d = cast('28 feb 2017' as datetime)

select @d,
       dateadd(m, -2, @d)-datepart(d, @d)+1



select * from myTable where
dTime <= @d and dTime >= dateadd(m, -2, @d)-datepart(d, @d)+1

Open in new window


Avoid using BETWEEN.
0
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

mell lian90Author Commented:
@Lee Savidge  ,

yes exactly ..
0
Vaibhav GoelMSBI , SQL ConsultantCommented:
Hello Mell Lian90

I have also done the same thing. Please try my solution also.  

Thank you

Vaibhav
0
mell lian90Author Commented:
@ Vaibhav Goel  , thanks for your answer , it's exactly what i need
0
mell lian90Author Commented:
the best solution
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
SQL

From novice to tech pro — start learning today.