Abhishek Ginani
Abhishek Ginani

Reputation: 4751

Condition inside case statement - SQL server 2008

How to use conditions inside case statement:

    select * from tbl
        where case when expr then
        (
         (x=@_megacity and state_group in (select * from temp_state_megacity)) OR
         (x=@_10lac and state_group in (select * from temp_state_10lac)) OR
         (x=@_below10 and state_group in (select * from temp_state_below10)) OR
         (x=@_rural and state_group in (select * from temp_state_rural))
        ) else true end

Error - Incorrect syntax

Upvotes: 1

Views: 519

Answers (1)

Thorsten Kettner
Thorsten Kettner

Reputation: 94884

In a WHERE clause you use AND , OR and NOT to combine expressions:

select * from tbl
    where NOT <expression> OR
    (
     (x=@_megacity and state_group in (select * from temp_state_megacity)) OR
     (x=@_10lac and state_group in (select * from temp_state_10lac)) OR
     (x=@_below10 and state_group in (select * from temp_state_below10)) OR
     (x=@_rural and state_group in (select * from temp_state_rural))
    );

Upvotes: 2

Related Questions