MariaDB COALESCE() Function

In MariaDB, COALESCE() is a built-in function that returns the first non-NULL value in the argument list.

MariaDB COALESCE() Syntax

Here is the syntax of the MariaDB COALESCE() function:

COALESCE(value1[, value2 ...])

Parameters

value1[, value2 ...]

Optional. parameter list. You should provide at least one parameter.

Return value

The MariaDB COALESCE() function returns the first value in the argument list that is not NULL a value. If all arguments are NULL, the function will return NULL.

If you COALESCE() provide arguments for , MySQL will return an error.

MariaDB COALESCE() Examples

Basic example

The following example shows the usage of the MariaDB CAST() function:

SELECT
  COALESCE(null, 'A'),
  COALESCE(null, 1, 'A');

result:

+---------------------+------------------------+
| COALESCE(null, 'A') | COALESCE(null, 1, 'A') |
+---------------------+------------------------+
| A                   | 1                      |
+---------------------+------------------------+

In this example, COALESCE(null, 'A') returned A, and returned 1.

All NULL

This MariaDB COALESCE() function will return NULL if all arguments are NULL.

SELECT COALESCE(NULL, NULL);

result:

+----------------------+
| COALESCE(NULL, NULL) |
+----------------------+
|                 NULL |
+----------------------+

Conclusion

In MariaDB, COALESCE() is a built-in function that returns the first non-NULL value in the argument list.

In some cases, you can use IFNULL() to achieve the same result.