Skip to main content
Version: V1.1.0

CASE

Declaration

CASE value WHEN compare-value THEN result [WHEN [compare-value] THEN result ...] [ELSE result] END
CASE WHEN condition THEN result [WHEN [condition] THEN result ...] [ELSE result] END

Description

In the first declaration, value = compare-value. In the second declaration, the result is true for the first condition. If no matching result value is found, the result is the value after ELSE. If there is no ELSE clause, the result is NULL.

Example

SELECT CASE 'B' WHEN 'A' THEN 1 WHEN 'B' THEN 2 END;
+----------------------------------------------+
| CASE 'B' WHEN 'A' THEN 1 WHEN 'B' THEN 2 END |
+----------------------------------------------+
| 2 |
+----------------------------------------------+
1 row in set (0.001 sec)

SELECT CASE CONCAT('A','B') WHEN CONCAT('AB','') THEN 'A' WHEN 'B' THEN 'B' END;
+--------------------------------------------------------------------------+
| CASE CONCAT('A','B') WHEN CONCAT('AB','') THEN 'A' WHEN 'B' THEN 'B' END |
+--------------------------------------------------------------------------+
| A |
+--------------------------------------------------------------------------+
1 row in set (0.001 sec)

SELECT CASE WHEN 1>0 THEN 'TRUE' ELSE 'FALSE' END;
+--------------------------------------------+
| CASE WHEN 1>0 THEN 'TRUE' ELSE 'FALSE' END |
+--------------------------------------------+
| TRUE |
+--------------------------------------------+
1 row in set (0.001 sec)