// conditional reference

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.

// 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.