In this tutorial, we will learn how to use IFNULL()
, ISNULL()
, COALESCE()
, and NVL()
Functions.
All the Functions returns the specified value if the value in the specified column inside these function is NULL
. In other words, All these functions are used to check the value is NULL
or not, if the values are NULL
, then specified value is return.
IFNULL
function is used in MySQL.
IFNULL(Expression, alternate_value);
Parameter | Description |
expression | Required. The expression to test whether is NULL |
alternate_value | Required. The value to return if an expression is NULL |
In MSSQL, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse, ISNULL
function is used.
ISNULL(Expression, alternate_value);
Parameter | Description |
expression | Required. The expression to test whether is NULL |
alternate_value | Required. The value to return if an expression is NULL |
ISNULL(Expression);
Parameter | Description |
expression | Required. The expression to test whether is NULL or not, if an expression is NULL, then ISNULL function return 1, otherwise it returns 0. |
MySQL COALESCE Function return the first non-NULL value of a list or return NULL if there are no non-NULL values in the list.
COALESCE(value1,value2,value3,...valueN)
Parameter | Description |
val1, val2, val_n | Required. The values to test. |
NVL
function is very similar to MySQL IFNULL
function and SQL Server ISNULL
. NVL
function is used to replace NULL
value to a specified value.
NVL(Expression,alternate_value);
Parameter | Description |
expression | Required. The expression to test whether is NULL |
alternate_value | Required. The value to return if an expression is NULL |
Let us consider this table "Employee" for records.
Table Name: Employee
ID | EmpName | City | Country | Gender | Salary |
1 | Shankar | Delhi | India | male | 25000 |
2 | Sourabh | Delhi | India | male | 30000 |
3 | Ranvijay | Mumbai | India | male | 15000 |
4 | Kapil | Noida | India | male | 25000 |
5 | Shalini | Jaipur | India | female | 18000 |
6 | Rakesh | Faridabad | India | male | 23000 |
7 | Akshay | Mumbai | India | male | 21000 |
8 | Sarah | New York | US | female | 76000 |
9 | Rocky | Noida | India | male | 28000 |
For selecting records if City and Country is NULL
, then it returns "n/a", otherwise it returns City and Country using IFNULL function, the query will be:
SELECT ID,EmpName,IFNULL(City,'n/a'),IFNULL(Country,'n/a')
FROM Employee;
ID | EmpName | City | Country |
1 | Shankar | Delhi | India |
2 | Sourabh | Delhi | India |
3 | Ranvijay | Mumbai | India |
4 | Kapil | n/a | India |
5 | Shalini | Jaipur | India |
6 | Rakesh | Faridabad | India |
7 | Akshay | Mumbai | India |
8 | Sarah | n/a | n/a |
9 | Rocky | Noida | India |
For selecting records if City and Country are NULL
, then it returns "n/a", otherwise it returns City and Country using ISNULL
function, the query will be:
SELECT ID,EmpName,ISNULL(City,'n/a'),ISNULL(Country,'n/a')
FROM Employee;
ID | EmpName | City | Country |
1 | Shankar | Delhi | India |
2 | Sourabh | Delhi | India |
3 | Ranvijay | Mumbai | India |
4 | Kapil | n/a | India |
5 | Shalini | Jaipur | India |
6 | Rakesh | Faridabad | India |
7 | Akshay | Mumbai | India |
8 | Sarah | n/a | n/a |
9 | Rocky | Noida | India |
For selecting records if City is NULL
, then it returns '0', otherwise it returns '0' and Country using MySQL ISNULL
function, the query will be:
SELECT ISNULL(City)
FROM Employee;
City |
0 |
0 |
0 |
1 |
0 |
0 |
0 |
0 |
0 |
For selecting records if City and Country are NULL, then it returns "n/a", otherwise it returns City and Country using COALESCE
function, the query will be:
SELECT ID,EmpName,COALESCE(City,'n/a'),COALESCE(Country,'n/a')
FROM Employee;
ID | EmpName | City | Country |
1 | Shankar | Delhi | India |
2 | Sourabh | Delhi | India |
3 | Ranvijay | Mumbai | India |
4 | Kapil | n/a | India |
5 | Shalini | Jaipur | India |
6 | Rakesh | Faridabad | India |
7 | Akshay | Mumbai | India |
8 | Sarah | n/a | n/a |
9 | Rocky | Noida | India |
For selecting records if City and Country are NULL
, then it returns "n/a", otherwise it returns City and Country using NVL
function, the query will be:
SELECT ID,EmpName,NVL(City,'n/a'),NVL(Country,'n/a')
FROM Employee;
ID | EmpName | City | Country |
1 | Shankar | Delhi | India |
2 | Sourabh | Delhi | India |
3 | Ranvijay | Mumbai | India |
4 | Kapil | n/a | India |
5 | Shalini | Jaipur | India |
6 | Rakesh | Faridabad | India |
7 | Akshay | Mumbai | India |
8 | Sarah | n/a | n/a |
9 | Rocky | Noida | India |