Comparative analysis of cases when and decode

Source: Internet
Author: User

First, case

Similar to If-else, the statement is as follows:
Case expr when Expr1 then RETURN_EXPR1
[When Expr2 then RETURN_EXPR2
...
When Exprn then RETURN_EXPRN
ELSE else_expr]
END

And there are two methods of judging, case field when value then return value

Else return value end

For example:

Select Bname, price, case at price > =10 and Price <20 then ' Price1 '

When Price > =20 and Price <30 then ' Price2 '
When the price >= and the price <40 then ' Price3 '

When Price > =40 and Price <50 then ' Price4 '

When the price >= and the price <60 then ' Price5 '

Else ' price6 ' End ' Price segment '
from book;

Second, decode (Oracle database Exclusive)

DECODE (Col|expression, Search1, RESULT1
[, SEARCH2, RESULT2,...,]
...
[, Searchn, Resultn,...,]
[, default])

can also be used with the sign function

can also: decode (field, judging condition, return value 1, return value 2)

Select Decode (ARG1-ARG2), -1, Arg1, arg2) from dual;

Note: The sign () function returns 0, 1, 1, depending on whether a value is 0, positive, or negative

Select Price,decode (Price, ' 32.5 ', ' alive ', ' other ') titles from the book;

Third, Comparison

1.DECODE is unique to Oracle;

2.CASE when is Oracle, SQL Server,mysql is available;

3.DECODE can only be used for equal judgment, but may match the sign function for greater than, less than, equal to the judgment; case can be applied to =,>=,<,<=,<>,is null,is not null;

4.DECODE uses it to be concise, while case is complex but more flexible.

Comparative analysis of cases when and decode

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.