SQL Where statment help

The last part of the WHERE needs to be different every month; the numbers behind "N##" correspond to a month.  They need to roll forward each month - the below script is setup for August, but for September, the N09 needs to change to N03; in October, the N10 would change to N04, and so on.   ' NS' doesn't change and is constant.

I've tried CASE, but can't get it to hit multiple fields.  One thought was to use a series of SQL jobs to update a temp table with the proper number series each month.  Another was to build some if/then into SSRS.

update purchase_class
set exclude_from_ranking = 'N', exclude_from_reclassification = 'N', date_last_modified = getdate(), last_maintained_by = 'ABIX_DBA'
where exclude_from_ranking = 'Y' and exclude_from_reclassification = 'Y'
and purchase_class_id in ('N11', 'N12', 'N01', 'N02', 'N09', 'N10', 'NS')
LVL 1
abarefootAsked:
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>but for September, the N09 needs to change to N03; in October, the N10 would change to N04, and so on.
Explain how N09 becomes N03.  Not getting it.
0
abarefootAuthor Commented:
This covers 6 months.  So September2014 would turn into March2015, October2014 would turn into April2015 and so on.
0
Scott PletcherSenior DBACommented:
Try code below.  Naturally you could substitute a variable for "GETDATE()" to make the reporting date more flexible.


and purchase_class_id in (
    SELECT 'N' + RIGHT('0' + CAST(ISNULL(NULLIF((MONTH(GETDATE()) + month_number) % 12, 0), 12) AS varchar(3)), 2)
    FROM ( SELECT 1 AS month_number UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL
           SELECT 5 UNION ALL SELECT 6 ) AS month_numbers
    UNION ALL
    SELECT 'NS'
)


To test it for other months, you can adjust the month# in the query.  For example, to test September:

...(NULLIF((MONTH(GETDATE()) + 1 + month_number) % 12, 0), 12) AS ...
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
awking00Commented:
Jim,
>but for September, the N09 needs to change to N03; in October, the N10 would change to N04, and so on.
Explain how N09 becomes N03.  Not getting it. <<
I found that rather confusing myself at first. He explains that the following was for August"
>>and purchase_class_id in ('N11', 'N12', 'N01', 'N02', 'N09', 'N10', 'NS')<<
which represents the six month numbers that would follow August, so the six month numbers that would follow September would be represented as  ('N10', 'N11', 'N12', 'N01', 'N02', 'N03', 'NS') having the effect of "replacing 09 with 03" At any rate, I think Scott's solution should do the trick. Here's another variation that should work -
with cte as
(select 'N' + right('00' + cast(datepart('mm',dateadd('mm',1, getdate())), as varchar(3),2) union all
 select 'N' + right('00' + cast(datepart('mm',dateadd('mm',2, getdate())), as varchar(3),2) union all
 select 'N' + right('00' + cast(datepart('mm',dateadd('mm',3, getdate())), as varchar(3),2) union all
 select 'N' + right('00' + cast(datepart('mm',dateadd('mm',4, getdate())), as varchar(3),2) union all
 select 'N' + right('00' + cast(datepart('mm',dateadd('mm',5, getdate())), as varchar(3),2) union all
 select 'N' + right('00' + cast(datepart('mm',dateadd('mm',6, getdate())), as varchar(3),2) union all
 select 'NS'
)
update ...
and purchase_class_id in
(select * from cte)
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

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.