Oracle several functions finishing DECODE () NVL NVL2 nullif COALESCE (RPM)

Source: Internet
Author: User
Tags mathematical functions

DECODE ()

Introduction to the Decode () function:

Main role: Translation of query results into other values (that is, in other forms of expression, the following examples);

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,

• Explanation of Meaning:

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;

The functions previously described in Oracle are string processing, date functions, mathematical functions, conversion functions, and so on, and a class of functions are general functions. Mainly: NVL,NVL2,NULLIF,COALESCE, these functions can be used on all types.

The following is a brief introduction to the use of several functions.

Before you introduce this, you have to understand what a null value in Oracle is NULL

1.NVL function

The format of the NVL function is as follows: NVL (EXPR1,EXPR2)

The meaning is: if the first parameter of Oracle is empty then the value of the second parameter is displayed, and if the value of the first parameter is not NULL, the first parameter is displayed.

For example:

Sql> Select ENAME,NVL (Comm,-1) from EMP;

Ename NVL (comm,-1)

------- ----

SMITH-1

ALLEN 300

WARD 500

JONES-1

MARTIN 1400

BLAKE-1

FORD-1

MILLER-1

Which shows that 1 of the original values are all null values

2 NVL2 function

The format of the NVL2 function is as follows: NVL2 (EXPR1,EXPR2, EXPR3)

The meaning is: if the first argument of the function is empty then the value of the second parameter is displayed, and if the value of the first parameter is not NULL, the value of the third parameter is displayed. Sql> Select ENAME,NVL2 (comm,-1,1) from EMP;

Ename NVL2 (comm,-1,1)

------- -----

SMITH 1

ALLEN-1

WARD-1

JONES 1

MARTIN-1

BLAKE 1

CLARK 1

SCOTT 1

In the example above. The result is 1 of the original is not empty, and the result is-1 of the original value is empty.

3. Nullif function

The effect of the Nullif (EXP1,EXPR2) function is to return null (NULL) if EXP1 and exp2 are equal, otherwise the first value is returned.
Here is an example. Using the HR schema in Oracle, if HR is locked, enable

The role here is to show the people who have changed their jobs and work.

Sql> Select E.last_name, E.job_id,j.job_id,nullif (e.job_id, j.job_id) "Old Job ID"

From Employees E, Job_history J

WHERE e.employee_id = j.employee_id

ORDER by Last_Name;

Last_Name job_id job_id Old JOB ID

----------------- ------- ------- -------

De Haan AD_VP It_prog AD_VP

Hartstein Mk_man Mk_rep Mk_man

Kaufling St_man St_clerk St_man

Kochhar AD_VP ac_mgr AD_VP

Kochhar AD_VP Ac_account AD_VP

raphaely Pu_man St_clerk Pu_man

Taylor Sa_rep Sa_man Sa_rep

Taylor Sa_rep Sa_rep

Whalen Ad_asst Ac_account Ad_asst

Whalen Ad_asst Ad_asst

You can see all the employee. job_id and job_histroy.job_id are equal, the output null in the result is empty, otherwise the employee is displayed. job_id

4.Coalesce function

The function of the Coalese function is somewhat similar to the functions of NVL, and its advantage is that there are more options.

The format is as follows:

COALESCE (Expr1, expr2, EXPR3 ... exprn)

Represents a placeholder that can specify multiple expressions. All expressions must be of the same type, or can be implicitly converted to the same type.
returns the first non-empty expression in the expression , such as the following statement: SELECT COALESCE (null,null,3,4,5) from dual its return result is: 3
If all arguments are NULL, COALESCE returns a null value. COALESCE (expression1,... N) is equivalent to this case function:
This function is actually a cyclic use of NVL, and this is not an example.

Oracle several functions finishing DECODE () NVL NVL2 nullif COALESCE (RPM)

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.