Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is it possible to have a case statement inside a when in case statement. Kinda tricky question. But it is as simple as having a case statement on the when clause of a case statement.
Quote:
An expression of non-boolean type specified in a context where a condition is expected


Am not really sure if what I've done so far will work.. but I wan't that to work that way.

What I have tried:

(CASE WHEN @variable = 1 THEN (CASE WHEN (CASE WHEN tbl_column_id IN(SELECT tbl_column_id FROM Table THEN 1 END)) AND (CASE WHEN othr_tbl_column_id NOT IN(SELECT othr_tbl_column_id FROM OtherTable THEN 0 END)) THEN 1 ELSE 0 END)
END)
Posted
Updated 7-Jun-18 6:12am
v3

Try something like this:
SQL
CASE 
    WHEN @variable = 1 THEN CASE 
        WHEN tbl_column_id IN (SELECT tbl_column_id FROM Table) AND othr_tbl_column_id IN (SELECT othr_tbl_column_id FROM OtherTable) THEN 1 
        ELSE 0 
    END
END
 
Share this answer
 
Comments
#realJSOP 7-Jun-18 13:12pm    
I came back to post exactly that... :)
Yes, you can use nested case statements, but it gets real ugly real fast. Try this:

SQL
CASE WHEN @variable = 1 
     THEN (CASE WHEN CASE WHEN tbl_column_id IN(SELECT tbl_column_id FROM Table) 
                          THEN 1 
                          ELSE 0 
                          END
                     AND CASE WHEN othr_tbl_column_id NOT IN(SELECT othr_tbl_column_id FROM OtherTable) 
                              THEN 0 
                              ELSE 1 
                              END 
      ELSE 0 END
 
Share this answer
 
v2
Comments
Richard Deeming 7-Jun-18 12:08pm    
I don't think that's going to work - you'll get the same "non-boolean" error as the code in the question. :)
#realJSOP 7-Jun-18 12:54pm    
Well, he had some misplaced parenthesis. I don't have a dataset I can throw that case at, so it's all conjecture anyway until someone runs it and sees...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900