SQLite ifnull

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

SQLite ifnull() function

Description

The ifnull() function returns a copy of its first non-NULL argument, or NULL if both arguments are NULL. Ifnull() must have exactly 2 arguments. 
The ifnull() function is equivalent to coalesce() with two arguments.

Syntax

ifnull(X,Y)

Example : SQLite ifnull() function

The following sqlite statement returns the first expression, i.e. 0, since the first expression is not NULL.

  1. SELECT ifnull(0,2);  
ifnull(0,2)
-----------
0

Example : ifnull() function with non zero 1st argument

The following SQLITE statement returns the first expression, i.e. 1, since the first expression is not NULL.

SELECT IFNULL(1,2);

  1. SELECT ifnull(1,2);  
ifnull(1,2)
-----------
1

Example : ifnull() function NULL

The following SQLITE statement returns the second expression, i.e. 2, since the first expression is NULL.

SELECT IFNULL(NULL,2);

  1. SELECT ifnull(NULL,2);  
ifnull(NULL,2)
--------------
2