Change sql statement depending on value

Hi,

I have an sql query looking like this:
SELECT cm.ClubMemberID, cm.subscriptionTypeID FROM dbo.ClubMember AS cm

Open in new window


Now I would like to expand the query by adding a WHERE clause. But I would like to alter the where clause depending of the subscriptionTypeID. So, for example, if subscriptionTypeID = 1 then the query would look like this:
SELECT cm.ClubMemberID, cm.subscriptionTypeID FROM dbo.ClubMember AS cm
WHERE cm.Stopdate > '2018-01-01'

Open in new window


and if subscriptionTypeID = 2 then
SELECT cm.ClubMemberID, cm.subscriptionTypeID FROM dbo.ClubMember AS cm
WHERE cm.Stopdate < '2018-01-01'

Open in new window


How can I achieve this scenario?

Thanks for help!

Peter
Peter NordbergIT ManagerAsked:
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.

Vikas GargAssociate Principal EngineerCommented:
Hello,

Please try the below query

SELECT cm.ClubMemberID, cm.subscriptionTypeID FROM dbo.ClubMember AS cm
WHERE 
case when subscriptionTypeID = 1 then
case when cm.Stopdate > '2018-01-01' then 1 else 0 end
else 
case when cm.Stopdate < '2018-01-01' then 1 else 0 end
end =1

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
Dorababu MSenior Software EngineerCommented:
Try this way some modification required

SELECT cm.ClubMemberID, cm.subscriptionTypeID FROM dbo.ClubMember AS cm
WHERE CM.subscriptionTypeID 
WHEN 1 THEN cm.Stopdate >  '2018-01-01' 
WHEN 2 THEN cm.Stopdate <  '2018-01-01'

Open in new window

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
T-SQL

From novice to tech pro — start learning today.