SQL IFNULL scalar function in DB2 for i SQL |
IFNULL
The IFNULL function returns the first nonnull expression.
Syntax
IFNULL(expression1, expression2)
IFNULL is identical to the COALESCE scalar function
Example#1:
SELECT IFNULL('TEST', ' ') FROM SYSIBM.SYSDUMMY1
Here, both the arguments are not null therefore, first argument will get return.
IFNULL TEST
Example#2:
SELECT IFNULL(NULL, 'A') FROM SYSIBM.SYSDUMMY1
Here, first argument is NULL and the second argument is not null therefore, second argument will get return since that is the first non-null argument.
IFNULL A
Example#3:
Student table has below record where name is null
SELECT * FROM student WHERE name is NULL
ROLLNO NAME SUBJECTID 1 - 205
Let's use below query that will return NULL as a result.
SELECT IFNULL(NULL, name) FROM student WHERE name is NULL
Here, first argument is NULL and the second argument will also return null therefore, NULL will be returned as no argument has Non-null value.
IFNULL -