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

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 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.19, see #10141), or translate your own on our website

References to this page

Feedback

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

The jOOQ Logo