Reputation: 3151
i would like to build a CASE statement that incorporates the following logic, but the sql compiler does not like the 'OR' in my statement:
CASE expression
WHEN expression1 OR expression2
THEN <yadda yadda>
ELSE <yadda yadda>
END
more specific code below:
CASE @var1
WHEN '99' OR '22'
THEN
(CASE @var2
WHEN 'All' THEN col1
ELSE @var2
END)
END
Upvotes: 12
Views: 18422
Reputation: 9180
DECLARE @Variable INT;
SET @Variable = 1;
SELECT
CASE
WHEN @Variable = 1 OR @Variable = 2 THEN 'It is 1 or 2'
WHEN @Variable = 3 THEN 'It is 3'
ELSE 'It is not 1, 2, or 3'
END AS [SomeField]
MSDN docs for CASE, OR, and Expressions.
Upvotes: 11
Reputation: 32720
Based on your edits, you don't even need an OR
statement:
CASE
WHEN @var1 IN ('99', '22')
THEN
(CASE @var2
WHEN 'All'
THEN col1
ELSE @var2
END)
END
Upvotes: 6