There are 2 types of CASE:
- General CASE
- CASE expression
-- General Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE result3 AS abc
END
-- Example
SELECT col_1, col_2,
CASE
WHEN col_2 = 33 THEN 'Yes!'
ELSE 'No!'
END
FROM table;Returns:
| col_1 | col_2 | case |
|---|---|---|
| 12 | 33 | Yes! |
| 22 | 211 | No! |
| 32 | 33 | Yes! |
-- General Syntax
CASE column
WHEN value1 THEN result1
WHEN value2 THEN result2
ELSE result3 AS abc
END
-- Example
SELECT col_1, col_2,
CASE col_2
WHEN 33 THEN 'Yes!'
ELSE 'No!'
END
FROM table;Returns:
| col_1 | col_2 | case |
|---|---|---|
| 12 | 33 | Yes! |
| 22 | 211 | No! |
| 32 | 33 | Yes! |
So the difference
| General Case | Expression Case |
|---|---|
| So much flexible allows any condition | Only allows the "equals to" condition |
| Need to write condition again and again for each case | No repeated condition writing saves time |