SQL Server
SELECT COALESCE(nickname, name) AS display_name FROM users;
ISNULL(nickname, name) is the T-SQL shorthand; its return type follows the first argument, COALESCE uses type precedence.
COALESCE everywhere; ISNULL, IFNULL and NVL are the same idea with local names.
Filed under Querying
Write COALESCE and the query runs everywhere; the shorthands only matter when reading existing code. One real behavioral difference to know: in SQL Server, ISNULL's result type comes from the first argument, so ISNULL(varchar_col, longer_default) silently truncates where COALESCE does not.
COALESCE(a, b, c) returns the first non-NULL argument and works in all five engines. Each engine also ships a two-argument version under a different name, which is the usual porting friction.
SELECT COALESCE(nickname, name) AS display_name FROM users;
ISNULL(nickname, name) is the T-SQL shorthand; its return type follows the first argument, COALESCE uses type precedence.
SELECT COALESCE(nickname, name) AS display_name FROM users;
COALESCE is the only built-in option, and the standard one.
SELECT IFNULL(nickname, name) AS display_name FROM users;
IFNULL is the two-argument shorthand; COALESCE takes many arguments.
SELECT IFNULL(nickname, name) AS display_name FROM users;
IFNULL and COALESCE are both built in; no NVL.
SELECT NVL(nickname, name) AS display_name FROM users;
NVL is the two-argument shorthand; NVL2(a, b, c) returns b when a is not NULL and c when it is.
COALESCE. It is standard, takes any number of arguments, and its result type follows the normal precedence rules. ISNULL, IFNULL and NVL are fine to read in existing code, but portable code uses COALESCE.
SQL Server expands COALESCE((SELECT …), 0) into CASE WHEN (SELECT …) IS NOT NULL THEN (SELECT …) ELSE 0 END, so the subquery can run twice. ISNULL((SELECT …), 0) evaluates it once. Oracle's NVL, by contrast, evaluates both arguments every time, while its COALESCE stops at the first non-NULL one.
Wrap the aggregate: COALESCE(SUM(x), 0). SUM over zero rows, or over only NULLs, returns NULL rather than 0. Writing SUM(COALESCE(x, 0)) does not fix that case, because SUM already skips NULL rows.
Fold NULL into the value order, or park it deliberately.
Same task, three operators, and the NULL rules differ between them.
NULL = NULL is unknown, not true, unless you use a null-safe operator.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.