Conditional Functions or Control Statements - (Case and Decode)

1) Case

Example

SELECT DEPARTMENT_ID,
CASE
WHEN DEPARTMENT_ID=10 THEN 'A'
WHEN DEPARTMENT_ID=20 THEN 'B'
ELSE 'X' END
FROM DEPARTMENTS;

DEPARTMENT_ID      C
-------------                   -----
        10                        A
        20                        B
        30                        X
        40                        X
        50                        X

2) Decode

This command is similar to if.. then.. else.. Statement. If the default statement is omitted then null will be used if no matches found.

Syntax

DECODE(COLUMN NAME, SERACH, RESULT, SEARCH, RESULT, DEFAULT)

Example

SELECT DEPARTMENT_ID, DECODE(DEPARTMENT_ID,10,'MANAGER',20,'ASST MANAGER',30,'PROJECT MANAGER',40,'TEAM LEADER','OTHERS') FROM DEPARTMENTS;

DEPARTMENT_ID          DECODE(DEPARTME
-------------------------          ----------------------------
           10                         MANAGER
           20                         ASST MANAGER
           30                         PROJECT MANAGER
           40                         TEAM LEADER
           50                         OTHERS
           60                         OTHERS
           70                         OTHERS
           80                         OTHERS
           90                         OTHERS
          100                        OTHERS


No comments:

Post a Comment