Sqlite Ifnull Function

SQLite: ifnull Function

This SQLite post explains how to use the SQLite ifnull function with syntax and examples.

Description

The SQLite ifnull function allows you to return an alternate value if expression1 is NULL. In other words, it will return the first non-null expression which is the same as using the coalesce function with 2 parameters.

Syntax

The syntax for the ifnull function in SQLite is:

ifnull( expression1, expression2 )

Parameters or Arguments

expression1 and expression2

Two values passed in where the first non-null value will be returned.

Note

  • If expression1 and expression2 evaluate to null, then the ifnull function will return null.

Applies To

The ifnull function can be used in the following versions of SQLite:

  • SQLite 3.8.6, SQLite 3.8.x, SQLite 3.7.x, SQLite 3.6.x

Example

Let's look at some SQLite ifnull function examples and explore how to use the ifnull function in SQLite.

For example:

sqlite> SELECT ifnull('AODBA.com', 'checkyoursite.com');
Output:  'AODBA.com'

sqlite> SELECT ifnull(NULL, 'checkyoursite.com');
Output:  'checkyoursite.com'

sqlite> SELECT ifnull('2014-10-16', '2014-10-31');
Output:  '2014-10-16'

sqlite> SELECT ifnull(null, '2014-10-31');
Output:  '2014-10-31'

sqlite> SELECT ifnull(5, 6);
Output:  5

sqlite> SELECT ifnull(null, 6);
Output:  6

sqlite> SELECT ifnull(5/0, 'Dividing by 0 returns NULL');
Output:  'Dividing by 0 returns NULL'