MySQL: COALESCE Function

MySQL COALESCE Function

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

This Post Has 2 Comments

Leave a Reply