DECODE

From Wikipedia, the free encyclopedia

DECODE is a single-row function in Oracle, which applies conditional logic to SQL queries. This function works very much like an IF-THEN-ELSE statement.

Syntax:

  DECODE (expression1, 
                        value1, return_value1,
                        value2, return_value2,
                       ...
                        valueN, return_valueN,
          [default_return_value])

[edit] How it works

If expression1 equals value1 then return_value1 is returned by DECODE function, and if expression1 equals value2 then return_value2 is returned and so on…..

The optional default_return_value is returned if expression1 does not match with any of (value1, value2, ..., valueN).

In short

IF expression1 = valueN   THEN return_valueN  ELSE default_return_value

Example:

 DECODE (day_id, 
                '1', 'Monday',
                '2', 'Tuesday',
                 ....
                '7', 'Sunday',
                     'Not Valid')