Link to home
Start Free TrialLog in
Avatar of Peter Nordberg
Peter NordbergFlag for Sweden

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Vikas Garg
Vikas Garg
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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