Dynamic date logic SQL

I have code that pulls any request where the date of the request is between 5-30 days old from today's date:
AND  (DAYS (CURRENT DATE) - Days (REQ.DT) BETWEEN 5 AND 30)

Now the group wants to change it to pull >4 and a rolling 12 months starting with the 1st date of the current month.

So if today is 11/20/15 then I would need to pull requests from 11/1/2014 - 11/15/2015

How can I do this without updating the code manually each time it is run?
DCUnitedAsked:
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.

Karthik RamachandranCommented:
This will do;
REQ.DT BETWEEN DATEADD(DD,-4,GETDATE()) AND ('01' + SUBSTRING(CONVERT(VARCHAR(10),CONVERT(VARCHAR(10),CONVERT(DATE,GETDATE(),101))),3,10))

Open in new window

0
Olaf DoschkeSoftware DeveloperCommented:
Without much further explanation:
Cast(RQQ.DT as Date) BETWEEN
Cast(DateAdd(yy,-1,DateAdd(dd,(1-Day(GetDate())),GetDate())) as Date)
And
Cast(DateAdd(dd,-5,GetDate()) as Date)

Open in new window


Just look up DateAdd in the books online

Bye, Olaf.
0
Karthik RamachandranCommented:
Here you go.
REQ.DT BETWEEN
DATEADD(DD,-4,GETDATE())
AND
('01/' + CONVERT(VARCHAR(2),DATEPART(MM,GETDATE())) + '/' + CONVERT(VARCHAR(4),(DATEPART(yyyy, GETDATE()) - 1)))
AND DATEADD(DD,-4,GETDATE())

Open in new window

0
Kevin CrossChief Technology OfficerCommented:
Hi.

The concept is shown above.  Use GETDATE() or CURRENT_TIMESTAMP with DATEADD function to manipulate the start and stop times you want.  However, please note when you add days to GETDATE(), you will bring forward the time stamp.  If the field you are comparing to is a DATETIME, it is better to use "dt >= start_date AND dt < end_date_plus_one".  If your date column just includes the date part (i.e., at midnight), you can use the new DATE data type by using CONVERT(DATE, DATEADD(DD, -X, GETDATE()).
0
Kevin CrossChief Technology OfficerCommented:
In your specific case, here is what I would recommend as a filter:
WHERE REQ.DT >= DATEADD(MM, DATEDIFF(MM, 0, GETDATE())-12, 0)
  AND REQ.DT < DATEADD(DD, DATEDIFF(DD, 0, GETDATE())-4, 0)

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
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 2008

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.