SoulRayder
SoulRayder

Reputation: 5166

Conditional Execution of conditions based on return value of CASE statement

I want to realize this functionality in SQL (basically b=2 and c in (3,4,5) are two conditions that I want to add to my select statement at the top level).

If a=1 then I want the first condition to be checked, else the second condition should be checked.

CASE 
  WHEN a=1 THEN {AND b=2}
  ELSE {AND c in (3,4,5)}

The total query is too complex for me to separately keep the entire query inside the THEN clause.

Upvotes: 2

Views: 68

Answers (1)

Robert Columbia
Robert Columbia

Reputation: 6418

SELECT Stuff
FROM Table
WHERE
SomeCondition
AND
(
     (a=1 AND b=2)
 OR
     ((NOT a=1) AND (c in (3,4,5)))
)

Upvotes: 3

Related Questions