This article briefly introduces the usage of the decode () function in oracle and illustrates the usage of this function in practical use using examples.
In Oracle/PLSQL, the DECODE function has the IF-THEN-ELSE statement function.
The syntax of the DECODE function is:
Decode (expression, search, result [, search, result]... [, default])
Compare the expression value.
Search is more valuable than expression.
Result is the returned value. If the expression is equal to search.
Default is optional. If no match is found, the default value is returned for decoding. If the default value is omitted, the decoding statement returns null (if no matching is found ).
Undo Modification
Applies:
Oracle 9i, Oracle 10g, Oracle 11g
Instance
SELECT supplier_name,
Decode (supplier_id, 10000, 'ibm ',
10001, 'Microsoft ',
10002, 'hett TT packard ',
'Gateway') result
FROM suppliers;
The preceding decoding statement 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: = 'hett TT Packard ';
ELSE
Result: = 'Gateway ';
End if;
The DECODE function compares the value of each supplier_id one by one.