SQL NVL function
Returns the second argument when the first is NULL, otherwise the first.
- 类别
- 条件
- Returns
same as first argument- In this category
- 7 of 43
语法
NVL(expr, default) | NVL(commission_pct, 0)
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| 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 | 是 | the only one of the five engines with NVL |
示例
Oracle
SELECT NVL(commission_pct, 0) FROM employees;
Oracle
SELECT NVL(NULL, 'fallback') FROM dual;
值得了解
- 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.
Related on SQLly
- Replacing NULL in output: COALESCE, ISNULL, NVL, IFNULL Dialect comparison
- Sorting with COALESCE in ORDER BY Dialect comparison
Cheat sheets for the engines that have it: Oracle.
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.