SQL ISNULL function
SQL Server's two-argument NULL substitution, typed by its first argument.
- 類別
- 條件
- Returns
same as first argument- In this category
- 9 of 43
語法
ISNULL(col, 0) | ISNULL(name, 'unknown')
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | 是 | Supported under this name. |
| PostgreSQL | Not built in | no ISNULL function; x ISNULL is a nonstandard postfix spelling of x IS NULL |
| MySQL | Not built in | MySQL's ISNULL(expr) takes one argument and returns 1 or 0 |
| SQLite | Not built in | no ISNULL function; x ISNULL is a postfix spelling of x IS NULL |
| Oracle | Not built in | use NVL |
範例
SQL Server
SELECT ISNULL(discount, 0) FROM orders;
SQL Server
SELECT ISNULL(CAST(NULL AS SMALLINT), 32767) AS fits, ISNULL(CAST(NULL AS SMALLINT), 50000) AS overflows; -- the second column raises error 220, so the statement fails
小知識
- The result type is the first argument's type: ISNULL(smallint_col, 50000) raises error 220 (arithmetic overflow for smallint) where COALESCE(smallint_col, 50000) widens to int. With strings it truncates instead: ISNULL(CAST(NULL AS varchar(3)), 'unknown') returns 'unk'.
- It takes exactly two arguments; COALESCE chains any number and is the standard.
- In MySQL the same name is a one-argument predicate returning 1 or 0: same word, different language.
Questions about ISNULL
ISNULL vs COALESCE on SQL Server?
ISNULL is typed by the first argument and takes two; COALESCE is standard, multi-argument and picks the highest-precedence type. Prefer COALESCE for new code.
What is ISNULL in MySQL?
A predicate: ISNULL(expr) returns 1 when expr is NULL, else 0.
Does Oracle have ISNULL?
No, it is NVL.
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
- Find unused index candidates Snippet
- Find the sessions consuming tempdb space in SQL Server Snippet
- Find the largest indexes in PostgreSQL Snippet
- Generate CREATE INDEX statements from SQL Server's missing index DMVs Snippet
- Track database growth by month from backup history in SQL Server Snippet
Cheat sheets for the engines that have it: SQL Server.
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.