Home > Net >  I'm getting the result as 1 in the output part. why it not executed second case ?? Explain this
I'm getting the result as 1 in the output part. why it not executed second case ?? Explain this

Time:10-08

SELECT  
    CASE 
        WHEN 1 = 1 THEN 1
        WHEN 1 = 1 THEN 2
        ELSE 0 
    END

Output: 1

Why does this CASE not return 2 as the result?

CodePudding user response:

As soon as the first condition is met (1 = 1), the result is being returned. Other conditions aren't evaluated so ... that's why you don't get 2 as result.

CodePudding user response:

The case clause is executed as If...elseif.. else condition. whenever the first condition in when satisfied, then result will show.

So first When (1=1) condition satisfy and give the then result always.

Try this below queries to know more

SELECT  CASE WHEN 1=2 THEN 1
    WHEN 1=1 THEN 2
    ELSE 0 END

SELECT  CASE WHEN 1=2 THEN 1
    WHEN 1=3 THEN 2
    ELSE 0 END
  •  Tags:  
  • sql
  • Related