In Oracle/plsql, the Decode function has a function of a if-then-else statement.
The syntax for the decode function is:
Decode (expression, search, result [, search, result] ... [, default])
The expression value is compared.
Search is the value of expression compared to.
Result is the value returned, if the expression equals search.
Default is optional. If no match is found, the decoding returns the default value. If the default is omitted, then the decoding statement returns null if no match is found.
Undo Changes
Applies To:
Oracle 9i, Oracle 10g, Oracle 11g
Instance
SELECT Supplier_name,
Decode (supplier_id, 10000, ' IBM '),
10001, ' Microsoft ',
10002, ' Hewlett Packard ',
' Gateway ') result
from suppliers;
The decoded statement above is equivalent to the following If-then-else statement:
IF supplier_id = 10000 THEN
Result: = ' IBM ';
elsif supplier_id = 10001 THEN
Result: = ' Microsoft ';
elsif supplier_id = 10002 THEN
Result: = ' Hewlett Packard ';
ELSE
Result: = ' Gateway ';
End IF;
The decode function compares each supplier_id value one after another.