MySQL: COALESCE Function
This MySQL tutorial explains 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 in MySQL 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 of MySQL:
- 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 MySQL COALESCE function examples and explore how to use the COALESCE function in MySQL.
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, 'techonthenet.com', 'checkyourmath.com'); Result: 'techonthenet.com' mysql> SELECT COALESCE(null, null, null, null, null); Result: NULL
Advertisements