SQLite ifnull() Function

A SQLite ifnull() function is a if-else function that returns the second parameter if the first parameter is NULL, and otherwise returns the first parameter.

Syntax

Here is the syntax of the SQLite ifnull() function:

ifnull(expr1, expr2)

The SQLite ifnull() function is equivalent to the coalesce(expr1, expr2) function with two parameters.

Parameters

expr1

Required. A value or expression to check if it is NULL.

expr2

Required. A value or expression.

Return value

If expr1 is NULL, the ifnull() function returns expr2, otherwise it returns expr1.

Examples

This example shows the basic usage of the SQLite ifnull() function:

SELECT
    ifnull(NULL, 'It is NULL'),
    ifnull('I am not NULL', 'x');
  ifnull(NULL, 'It is NULL') = It is NULL
ifnull('I am not NULL', 'x') = I am not NULL