Comparison of Oracle Decode () function and Case statement

Source: Internet
Author: User
Tags case statement comparison

Oracle Decode () functions and case statements are all common use, so what is the difference between them? Here's a detailed description of

The difference between the Oracle Decode () function and the case statement for your reference.

First, give 2 simple examples to compare the differences between the 2.

1.CASE statement:

The following is a code fragment:

SELECT Case SIGN (5-5)





when 1 THEN ' is Positive '





WHEN-1 THEN ' is Negative '





ELSE ' is Zero ' end





from DUAL;

Background implementation:

The following is a code fragment:

if (SIGN (5–5) = 1) {





' is Positive ';





} else if (SIGN (5–5) = 2) {





' is Negative ';





}else {





' is Zero '





}

2. Decode function:

The following is a code fragment:

SELECT DECODE (SIGN (5–5), 1,





' is Positive ',-1, ' are Negative ', ' is Zero ')





fromdual

Background implementation:

The following is a code fragment:

switch (SIGN (5–5))





{





Case 1: ' Is Positive '; Break





Case 2: ' Is Negative '; Break





default: ' Is Zero '





}

In the above example, 2 of them seem to be achievable. However, Decode () is quite complex to implement when encountering extraordinary problems.

For example:

The following is a code fragment:

SELECT Case X-field





when X-field < THEN ' X-field < 40 '





when X-field < THEN ' X-field < 50 '





When X-field < THEN ' X-field < 60 '





ELSE ' Unbeknown ' end





from DUAL

Therefore, the individual considers that the case statement is very flexible in dealing with similar problems. When you just need to match a small number of values, use decode more succinctly.

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.