Available in versions: Dev (3.20) | Latest (3.19) | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11 | 3.10

NVL2

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

The NVL2() function checks if the first argument is NOT NULL to produce the second argument, or the third argument otherwise. It works in a similar way as the IIF function or the CASE expression

SELECT
  nvl2(1,    2, 3),
  nvl2(null, 2, 3);
create.select(
  nvl2(val(1)             , 2, 3),
  nvl2(val((Integer) null), 2, 3)).fetch();

The result being

+------+------+
| nvl2 | nvl2 |
+------+------+
|    2 |    3 |
+------+------+

Dialect support

This example using jOOQ:

nvl2(val(1), 2, 3)

Translates to the following dialect specific expressions:

Access, SQLServer

iif(1 IS NOT NULL, 2, 3)

ASE, Aurora MySQL, Aurora Postgres, BigQuery, CockroachDB, Derby, DuckDB, Firebird, Hana, MemSQL, MySQL, Postgres, SQLDataWarehouse, SQLite, Sybase, Trino, YugabyteDB

CASE
  WHEN 1 IS NOT NULL THEN 2
  ELSE 3
END

DB2, Exasol, H2, HSQLDB, Informix, MariaDB, Oracle, Redshift, Snowflake, Teradata, Vertica

nvl2(1, 2, 3)

(These are currently generated with jOOQ 3.20, see #10141), or translate your own on our website

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo