Home > Oracle, SQL > Oracle – PL/SQL decode function

Oracle – PL/SQL decode function

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.

Advertisements
  1. Oleg
    October 7, 2013 at 3:45 AM

    Mate, you can’t use DECODE in PL/SQL code

    PLS-00204: function or pseudo-column ‘DECODE’ may be used inside a SQL statement only

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: