Archive

Posts Tagged ‘pl/sql decode’

Oracle – PL/SQL decode function

August 19, 2011 1 comment

The decode function acts like IF…THEN…ELSE and can be used within PL/SQL code and SQL code much the same.

The syntax is as follows:

decode(expression, search, result [, search , result]... [, default] )

Example:

SELECT employee_name
DECODE(employee_id, 1, 'Alvin'
2, 'Simon'
Theodore') result
FROM employees;

The above code acts like the following:

IF employee_id = 1 THEN
result:= 'Alvin'
ELSIF employee_id = 2 THEN
result:= 'Simon'
ELSE
result:= 'Theodore'
END IF;

The decode will compare each employee_id one by one.