Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL SYNTAX ISSUE - using OR in CASE statement

Tags:

syntax

tsql

case

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
like image 689
sion_corn Avatar asked Oct 02 '12 19:10

sion_corn


1 Answers

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.

like image 75
Seth Flowers Avatar answered Oct 01 '22 02:10

Seth Flowers