The NVL and NVL2 functions in DB2

Source: Internet
Author: User
Tags db2



The NVL function is a null-value conversion function


NVL (Expression 1, Expression 2)


If expression 1 is a null value, NVL returns the value of expression 2, otherwise returns the value of expression 1.
The purpose of this function is to convert a null value (NULL) into an actual value. The value of its expression can be numeric, character, and date. But the data type of expression 1 and expression 2 must be the same type.


    • For digital type: NVL (a,0);
    • For character type: NVL (To_char (a), ' Zifeiy ')
    • For date type: NVL (mydate, ' 31-dec-99 ')


Practical Examples:
Query for an employee's annual salary, if empty, replace it with 0


select (sal+nvl(comm,0))*12fromwhere emp.pno=:pno;


Here, Comm is an undefined variable that refers to a numeric type. NVL returns to 0.


NVL2 (Expression 1, Expression 2, Expression 3)


If expression 1 is empty, the return value is the value of expression 3. If expression 1 is not empty, the return value is the value of expression 2.
For example:


NVL2(comm,‘sal+comm‘,sal)


NVL2 function Test Comm
If comm is empty, the value of the SAL is returned. If Comm is not empty (null), the value of the expression Sal+comm is returned.



The NVL and NVL2 functions in DB2


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.