Comparison between OracleDecode () and CASE statements

Source: Internet
Author: User
OracleDecode () Functions and CASE statements are often used. What are their differences? The following describes the differences between the OracleDecode () function and the CASE statement for your reference. First, let's give two simple examples to briefly compare the differences between the two. 1. CASE statement: The following is a code snippet: SELECTCASESIGN (5-5) W

Oracle Decode () Functions and CASE statements are often used. What are their differences? The following describes the differences between the Oracle Decode () function and the CASE statement for your reference. First, let's give two simple examples to briefly compare the differences between the two. 1. CASE statement: The following is a code snippet: SELECTCASESIGN (5-5) W

Oracle Decode () Functions and CASE statements are often used. What are their differences? The following is a detailed introduction

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

First, let's give two simple examples to briefly compare the differences between the two.

1. CASE statement:

The following is a code snippet:

Select case sign (5-5)

WHEN 1 THEN 'is Positive'

WHEN-1 THEN 'Is Negative'

ELSE 'is 0' END

From dual;

Background implementation:

The following is a code snippet:

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 snippet:

Select decode (SIGN (5-5), 1,

Is Positive,-1, Is Negative, Is Zero ')

FROMDUAL

Background implementation:

The following is a code snippet:

Switch (SIGN (5-5 ))

{

Case 1: 'Is Positive '; break;

Case 2: 'Is Negative '; break;

Default: 'Is 0'

}

In the above example, both of them can be implemented. However, the implementation of Decode () is quite complicated when encountering extraordinary problems.

For example:

The following is a code snippet:

The select case X-FIELD

WHEN X-FIELD <40 THEN 'x-FIELD <40'

WHEN X-FIELD <50 THEN 'x-FIELD <50'

WHEN X-FIELD <60 THEN 'x-FIELD <60'

ELSE 'unbeknown 'END

FROM DUAL

Therefore, I personally think that CASE statements are flexible in handling similar problems. Decode is more concise to match a small number of values.

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.