totn MariaDB Functions

MariaDB: COALESCE Function

This MariaDB tutorial explains how to use the MariaDB COALESCE function with syntax and examples.

Description

The MariaDB COALESCE function returns the first NON-NULL expression in the list.

Syntax

The syntax for the COALESCE function in MariaDB is:

COALESCE( expression1, expression2, ... expression_n )

Parameters or Arguments

expression1, expression2, ... 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 MariaDB:

  • MariaDB 10

Example

Let's look at some MariaDB COALESCE function examples and explore how to use the COALESCE function in MariaDB.

For example:

SELECT COALESCE(NULL, 'TechOnTheNet.com', 'CheckYourMath.com');
Result: 'TechOnTheNet.com'

SELECT COALESCE(NULL, NULL, NULL, 'Tech', 'On', 'The', 'Net');
Result: 'Tech'

SELECT COALESCE('Tech', 'On', NULL, 'The', 'Net');
Result: 'Tech'

SELECT COALESCE(NULL, 1, 2, 3, NULL, 4);
Result: 1

SELECT COALESCE(NULL, NULL, NULL, NULL, NULL);
Result: NULL