SQL CASE STATEMENT IN A CASE STATEMENT

1.5k Views Asked by At

How do I make a case statement inside another case statement. I need to find when condition1 = 77, then column value = y only if the y value is equal to 3. otherwise the value should be condition3

I tried coding it like this:

CASE 
    WHEN confition1 LIKE '77' 
        THEN condition2 
CASE 
    WHEN condition2 LIKE '3' 
        THEN condition2 
ELSE condition3 
ELSE condition3 
        
2

There are 2 best solutions below

1
Monofuse On
DECLARE @test INT = 2
DECLARE @test2 INT = 3

SELECT CASE 
    WHEN @test = 1 THEN 'One'
    WHEN @test = 2 THEN (CASE 
        WHEN @test2 = 2 THEN 'Two'
        ELSE 'Not Two' END)
    ELSE 'Unknown' END 
0
karagoz On

If I understand correctly, the following code will work

case when confition1 like '77' and condition2 LIKE '3' then condition2 
else condition3 end