SELECT using 'CASE' in SQL SELECT using 'CASE' in SQL sql sql

SELECT using 'CASE' in SQL


This is just the syntax of the case statement, it looks like this.

SELECT   CASE     WHEN FRUIT = 'A' THEN 'APPLE'     WHEN FRUIT = 'B' THEN 'BANANA'       END AS FRUITFROM FRUIT_TABLE;

As a reminder remember; no assignment is performed the value becomes the column contents. (If you wanted to assign that to a variable you would put it before the CASE statement).


Change to:

SELECT   CASE     WHEN FRUIT = 'A' THEN 'APPLE'     WHEN FRUIT = 'B' THEN 'BANANA'       ENDFROM FRUIT_TABLE;


Try this.

SELECT   CASE      WHEN FRUIT = 'A' THEN 'APPLE'     WHEN FRUIT = 'B' THEN 'BANANA'     ELSE 'UNKNOWN FRUIT'  END AS FRUITFROM FRUIT_TABLE;