SQLite 3 reference · Conditional and Type Functions

SQLite NULLIF() Function

Return NULL if two values are equal, otherwise the first value. Often used to avoid division by zero.

SQLite 3

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

nullif(X, Y)

Parameters

ParameterDescription
X The value.
Y The value that should become NULL.

Return value

NULL if X = Y, otherwise X.

Examples

Equal values become NULLTry it yourself
SELECT nullif(5, 5) AS same, nullif(5, 3) AS different;

Output

same | different
NULL | 5
Avoid dividing by zeroTry it yourself
SELECT name, stock, round(100.0 / nullif(stock, 0), 2) AS pct_per_unit
FROM products
WHERE product_id IN (2, 3);

Output

name | stock | pct_per_unit
Clean Code | 15 | 6.67
The Pragmatic Programmer | 0 | NULL

In other languages

Was this page helpful?