MySQL IFNULL() function
IFNULL() function
MySQL IFNULL() takes two expressions and if the first expression is not NULL, it returns the first expression. Otherwise, it returns the second expression.
Depending on the context in which it is used, it returns either numeric or string value.
Syntax:
IFNULL(expression1, expression2);
Arguments:
Name | Description |
---|---|
expression1 | An expression. |
expression2 | An expression. |
MySQL Version: 5.6
Example: MySQL IFNULL() function
The following MySQL statement returns the first expression, i.e. 0, since the first expression is not NULL.
Code:
SELECT IFNULL(0,2);
Sample Output:
mysql> SELECT IFNULL(0,2); +-------------+ | IFNULL(0,2) | +-------------+ | 0 | +-------------+ 1 row in set (0.03 sec)
Example: IFNULL() function with non zero 1st argument
The following MySQL statement returns the first expression, i.e. 1, since the first expression is not NULL.
Code:
SELECT IFNULL(1,2);
Sample Output:
mysql> SELECT IFNULL(1,2); +-------------+ | IFNULL(1,2) | +-------------+ | 1 | +-------------+ 1 row in set (0.00 sec)
Example: IFNULL() function NULL
The following MySQL statement returns the second expression, i.e. 2, since the first expression is NULL.
Code:
SELECT IFNULL(NULL,2);
Sample Output:
mysql> SELECT IFNULL(NULL,2); +----------------+ | IFNULL(NULL,2) | +----------------+ | 2 | +----------------+ 1 row in set (0.00 sec)
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook