The CASE statement is similar to the IF ELSE statement in other programming languages. We can use this to get or show particular values based on certain conditions.
Example:
Take a table student:
| id | name | gender |
|---|---|---|
| 1 | Rahul | M |
| 2 | Veronica | F |
| 3 | Raj | M |
| 4 | Preeti | F |
| 5 | Ramesh | M |
| 6 | Amit | M |
Now we will be going to implement a CASE statement to get gender Male for 'M', Female for 'F', and Other if nothing is defined.
SELECT id, name, CASE WHEN gender='M' THEN 'Male' WHEN gender='F' THEN 'Female' ELSE 'Other' END AS genderFROM student;Output Table:
| id | name | gender |
|---|---|---|
| 1 | Rahul | Male |
| 2 | Veronica | Female |
| 3 | Raj | Male |
| 4 | Preeti | Female |
| 5 | Ramesh | Male |
| 6 | Amit | Male |

0 Comments