// conditional reference

SQL NVL function

Returns the second argument when the first is NULL, otherwise the first.

Kategorie
Bedingt
Returns
same as first argument
In this category
7 of 43

Syntax

NVL(expr, default) | NVL(commission_pct, 0)

Engine-Unterstützung

EngineSupportNotizen
SQL Server Not built in No built-in equivalent.
PostgreSQL Not built in No built-in equivalent.
MySQL Not built in No built-in equivalent.
SQLite Not built in No built-in equivalent.
Oracle Ja the only one of the five engines with NVL

Beispiele

Oracle

SELECT NVL(commission_pct, 0) FROM employees;

Oracle

SELECT NVL(NULL, 'fallback') FROM dual;

Gut zu wissen

  • NVL evaluates both arguments; COALESCE short-circuits and stops at the first non-NULL, which matters when the default is expensive or has side effects.
  • NVL converts the second argument to the first argument's type: NVL(number_col, 'n/a') fails with ORA-01722 when it tries to turn 'n/a' into a number. COALESCE refuses mixed type families up front with ORA-00932.
  • Elsewhere: IFNULL in MySQL and SQLite, ISNULL in SQL Server, COALESCE everywhere.

Questions about NVL

NVL vs COALESCE in Oracle?

Two arguments versus many, eager versus short-circuit evaluation, and NVL's implicit conversion of the second argument versus COALESCE's same-type requirement. COALESCE is the standard and safer default.

What is NVL in MySQL?

IFNULL(expr, default), same behaviour.

Does NVL handle more than two values?

No; nest it or use COALESCE.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.