In this guide, we will explain how to use the MySQL COALESCE function with syntax and examples.
Description
The MySQL COALESCE function returns the first non-null expression in the list.
Syntax
The syntax for the COALESCE function is:
COALESCE( expression1, expression2, ... expression_n )
Parameters or Arguments
expression1 to expression_n
The expressions to test for non-null values.
Note
- If all expressions evaluate to null, then the COALESCE function will return null.
Applies To
The COALESCE 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 COALESCE function examples and explore how to use the COALESCE function.
For example:
mysql> SELECT COALESCE(null, null, null, 'A', 'B'); Result: 'A' mysql> SELECT COALESCE('A', 'B', null, 'C', 'D'); Result: 'A' mysql> SELECT COALESCE(null, 1, 2, 3, null, 4); Result: 1 mysql> SELECT COALESCE(null, 'adglob.in', 'baldevinfo.com'); Result: 'adglob.in' mysql> SELECT COALESCE(null, null, null, null, null); Result: NULL
Next Topic : Click Here
Pingback: MySQL: CAST Function | Adglob Infosystem Pvt Ltd
I value the article.Really thank you! Really Cool.