Oracle decode functions and sign functions

Source: Internet
Author: User

Process Control function DECODE

Introduction to the Decode () function:

Main functions:

Translate the results of the query into other values (i.e. in other forms, as illustrated below);

How to use:

Select decode (ColumnName, value 1, translation value 1, value 2, translation value 2,... Value n, translation value n, default value)

From Talbename

Where ...

Where ColumnName is the column defined in the table to be selected,

Meaning explanation:

Decode (condition, value 1, translation value 1, value 2, translation value 2,... The value n, the translation value n, the default value) is understood as follows:

if (condition = = value 1)

Then

return (translation value 1)

elsif (Condition = = value 2)

Then

Return (translation value 2)

......

elsif (Condition = = value N)

Then

return (translated value n)

Else

return (default value)

End If

Note: The default value can be either the column name you want to select, or the other values you want to define, such as other;

To illustrate:

Now defines a table named output, where two column definitions are Monthid (VAR) and sale (number type), translated to d,=2000 when translated to c,=3000 when sale value =1000 translation to a , if other values are translated as other;

SQL is as follows:

Select Monthid,decode (sale,1000, ' D ', +, ' C ', +, ' B ', 4000, ' A ', ' other ') sale from output

Special cases:

If you are comparing with only one value

Select Monthid, decode (sale, NULL, '---', sale) sale from output

Another: Decode can use other functions, such as the NVL function or the sign () function, etc.

Compare size function Sign

function Syntax:
Sign (n)

Function Description:
Take the sign of the number n, greater than 0 returns 1, less than 0 returns-1, equals 0 returns 0

Example:
1. Select sign (+), sign (-+), sign (0) from dual;

Sign ( -100) sign (0)
———- ———- ———-
1-1 0

2, A=10,b=20
Sign (A-B) returns-1

NVL (EXPR1,EXPR2)

If EXPR1 is null, the EXPR2 is returned, otherwise EXPR1 is returned.

SELECT Name,nvl (To_char (COMM), ' not application ') from TABLE1;

If you use the Decode function, it's

Select Monthid,decode (NVL (sale,6000), 6000, ' NG ', ' OK ') from output

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

If you take a smaller value, it's

Select Monthid,decode (sign (sale-6000), -1,sale,6000) is the goal of taking a smaller value from output.

Oracle decode functions and sign functions

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.