In this guide, we will explain how to use the MySQL IFNULL function with syntax and examples.
Description
The MySQL IFNULL function allows you to return an alternate value if an expression is NULL.
Syntax
The syntax for the IFNULL function is:
IFNULL( expression, value_if_null )
Parameters or Arguments
expression
The value to test as NULL.
value_if_null
The value to return if expression is NULL.
Note
- The IFNULL function will return expression, if expression is NOT NULL.
- The IFNULL function will return value_if_null, if expression is NULL.
- The IFNULL function is similar to the Nz function in MSAccess.
Applies To
The IFNULL function can be used in the following versions :
- MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23
Example
Let’s look at some IFNULL function examples and explore how to use the IFNULL function.
For example:
mysql> SELECT IFNULL('adglob.in', 'baldevinfo.com'); Result: 'adglob.in' mysql> SELECT IFNULL(NULL, 'baldevinfo.com'); Result: 'baldevinfo.com' mysql> SELECT IFNULL(DATE('2014-02-01'), '2014-02-18'); Result: '2014-02-01' mysql> SELECT IFNULL(DATE(NULL), '2014-02-18'); Result: '2014-02-18' mysql> SELECT IFNULL(5, 6); Result: 5 mysql> SELECT IFNULL(5/0, 'Dividing by 0 returns NULL'); Result: 'Dividing by 0 returns NULL'
Next Topic : Click Here
Pingback: MySQL: IF Function | Adglob Infosystem Pvt Ltd