Netezza NVL and NVL2 Functions with Examples

  • Post author:
  • Post last modified:July 27, 2018
  • Post category:Netezza
  • Reading time:2 mins read

Netezza supports NVL and NVL2 functions.

Netezza NVL Function

The NVL function replaces a NULL value with a replacement string that you provide in the function. Returns the first argument if it is not null, otherwise it returns the second argument

The Netezza NVL function is equivalent to the Netezza SQL coalesce function. The Function will return the non-NULL value.

Note that, You cannot replace the blank value using NVL function. Some databases supports this nut Netezza does not.

Syntax:

NVL (exp,replacement-exp);

Returns ‘exp’ if not null otherwise ‘replacement-exp’ is returned.

Netezza NVL Function Example

Below is the example on usage of Netezza NVL function:

SYSTEM.ADMIN(ADMIN)=> select price, NVL(price, 0.0) as price1 from (select NULL as price) a; 
 PRICE | PRICE1 
-------+---------- 
 | 0.000000 
(1 row)

Netezza NVL2 Function

Netezza NVL2 Function returns the second argument if the first argument is not null, otherwise it returns the third argument. Netezza NVL2 is short hand for the Netezza case expression.

Read:

Syntax:

NVL2 (exp,replacement-exp1,replacement-exp2);

Returns ‘replacement-exp1’ if ‘exp’ is not null, otherwise ‘replacement-exp2’ is returned.

Netezza NVL2 Function Examples

SYSTEM.ADMIN(ADMIN)=> select price,curr, NVL2(price, 0.0,1) as price1, NVL2(curr,1,0) as curr1 from (select NULL as price,1 as curr) a; 
 PRICE | CURR | PRICE1 | CURR1 
-------+------+--------+------- 
 | 1 | 1 | 1 
(1 row)

This Post Has 2 Comments

  1. Suresh Raj

    Thanks for posting this, it is very useful
    Can you please update the syntax from
    NVL2 (exp,replacement-exp1,replacement-exp1);
    to
    NVL2 (exp,replacement-exp1,replacement-exp2);

    1. Vithal S

      Thank you Suresh, Updated syntax 🙂

Comments are closed.