1

I am trying to do some case statement in my SQL. Below example shows what I want to do.

Example

case WHEN p = 1 THEN 'SOMETHING' 
     WHEN p = 1 AND q = 1 THEN 'SOMETHING ELSE'

Here, CASE returns the result of the first WHEN clause that evaluates to true. In this example , the first and second WHEN conditions are true, but returns the result of the first one.

Nirav Patel
  • 1,297
  • 1
  • 12
  • 23
  • Yes. What is your question? – GSerg Jan 04 '19 at 20:44
  • Its a `if and else case` not an `if in if` case thaf means if the top is true then theres no need to go further rather in the latter if first is true then again check if it meets further more conditions (if in if) as in your case `CASE WHEN p=1 Then (Case When q=1 Then "correct" End Case ) Else "incorrect" End Case ` – Himanshu Jan 04 '19 at 21:09

1 Answers1

4

CASE is always executed sequentially. Just change the order of the WHEN clauses to get what you want:

case WHEN p = 1 AND q = 1 THEN 'SOMETHING ELSE'
     WHEN p = 1 THEN 'SOMETHING' 
end
The Impaler
  • 45,731
  • 9
  • 39
  • 76