Decode () function usage in Oracle/plsql

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.