1-- NOTE: this is for SQL-Oracle specifically
2
3/*
4NB: Please like Mingles444 post, I derived this from him/her
5*/
6
7-- syntax: (Retrieved from grepper:Mingles444)
8CASE
9 WHEN condition1 THEN result1
10 WHEN condition2 THEN result2
11 WHEN conditionN THEN resultN
12 ELSE result
13END
14
15-- example:
16SELECT
17 CASE
18 WHEN (1+6 = 6) THEN 'A'
19 WHEN (1+6 = 7) THEN 'B'
20 WHEN (1+6 = 8) THEN 'C'
21 ELSE 'D'
22 END
23FROM DUAL;
24
25-- OUTPUT: B
26
1-- Syntax for SQL Server, Azure SQL Database and Azure Synapse Analytics
2
3--Simple CASE expression:
4CASE input_expression
5 WHEN when_expression THEN result_expression [ ...n ]
6 [ ELSE else_result_expression ]
7END
8
9--Searched CASE expression:
10CASE
11 WHEN Boolean_expression THEN result_expression [ ...n ]
12 [ ELSE else_result_expression ]
13END
14
1/*CASE statements are used to create different outputs and is
2 used by SQL as a way to handle if-then logic.*/
3
4 SELECT column_name,
5 CASE
6 WHEN condition THEN 'Result_1'
7 WHEN condition THEN 'Result_2'
8 ELSE 'Result_3'
9 END
10 FROM table_name;