Home / SQL / Reference / Numbers and NULL

SQL NULLIF

Return NULL if a equals b, otherwise a.

Syntax

NULLIF(a, b)

Mostly used to avoid dividing by zero: x / NULLIF(y, 0) gives NULL instead of an error.

Example

Query (on the shop database used in the SQL course)

SELECT 10 / NULLIF(0, 0) AS safe_division,
       NULLIF('n/a', 'n/a') AS cleaned;

Result

safe_divisioncleaned
NULLNULL

1 row

Related

Learn it step by step in the free lesson Functions: Text, Numbers, Dates & CASE, with exercises that check your queries.