NVL and NVL2 functions in DB2

The NVL function is a null conversion function

NVL(表达式1,表达式2)

If expression1 is null, NVL returns the value of expression2, otherwise it returns the value of expression1.
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 expression1 and expression2 must be the same type.

  • For numbers: NVL(a,0);
  • For character type: NVL(TO_CHAR(a), 'zifeiy')
  • For date type: NVL(mydate,'31-DEC-99')

Practical example:
Query the annual salary of an employee, if it is empty, replace it with 0

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

Here, comm is an undefined variable that refers to a numeric type. nvl returns 0.

NVL2(表达式1,表达式2,表达式3)

If expression1 is empty, the return value is the value of expression3. If expression1 is not empty, the return value is the value of expression2.
E.g:

NVL2(comm,'sal+comm',sal)

The NVL2 function tests comm
and returns the value of sal if comm is empty. If comm is not empty (null), return the value of the expression sal+comm.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325027823&siteId=291194637
db2
db2