Oracle functions related to null values

Source: Internet
Author: User
Environment: At the beginning, Oracle10g came into contact with the pascal language, so sometimes it is not very fond of the C language syntax. There is a lazy way of writing in C Language (also exists in java)

Environment: Oracle 10g was initially exposed to the pascal language, so sometimes it is not very fond of the C language syntax. There is a lazy way of writing in C Language (also exists in java)

Environment: Oracle 10g

In the first place, we came into contact with the pascal Language. Therefore, sometimes we do not like the C language syntax very much.
There is a lazy way of writing in C Language (also exists in java), probably like this: var: = (a> = B? );
I don't like it very much.
But the fact is, this can indeed be a lazy and already exists, so you have to adapt to it.
PL/SQL does not have this syntax, nor does SQL. PL/SQL is similar to PASCAL's syntax, but they have functions that implement similar functions, functions with null values include.

1) nvl (a, B)
The most classic and simple ones are not detailed. They can be used anywhere. SELECT clause and WHERE clause.

2) lnnvl ()
A is an expression.
Lnnvl can only be used in the where clause. The operator symbols of an expression cannot contain AND, OR,.
If the result of a is false or unknown, lnnvl returns true. If the result of a is true, false is returned.
"If the result of a is false or unknown, lnnvl returns true." This is very important because when a null value or unknown value is compared with another constant or variable with a value, the returned result is always unknown, so
Lnnvl (a> 10) is equivalent to nvl (a, 0) <= 10 is equivalent to a <= 10 or a is null (assuming a number (10 ))
To put it bluntly, lnnvl is a specific function used to simplify expressions. During orcle parsing, it should be parsed as "a <= 10 or a is null ".

3) nullif
This function is similar to PL/SQL for a long time.
Format: nullif (expr1, expr2) is equivalent to "case when expr1 = expr 2 then null else expr1 END ",
Restriction: expr1 cannot be the identifier null. If you enter nullif (null, expr2), an error is returned.
Both expr1 and expr2 must be a variable or a constant expression, not a logical expression.
4) nvl2
It is also a function similar to the C language format.
Format: NVL2 (EXPR1, EXPR2, EXPR3)
Equivalent to: case when expr1 is null then expr2 else expr3 end
Restrictions: 1) expr1 can be of any type. expr2 and expr3 cannot be of the long type.
2) If expr2 is of the character type, convert expr3 to the character type before comparison (except null ).
3) If expr2 is a numerical value, expr3 is also converted to the corresponding numerical type.
4) each parameter cannot be a logical expression.

Conclusion: 1) Other functions can be converted to nvl or case when format.
2) In addition to convenience, it is enough to know only nvl and case when.
3) If you can, it can be used occasionally for convenience.

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.