MySQL COALESCE Function

MySQL COALESCE Function

 MySQL COALESCE Function



Summary: This tutorial introduces you to the MySQL COALESCE function that allows you to substitute NULL values.

Introduction to MySQL COALESCE function

The following illustrates the COALESCE function syntax:

COALESCE(value1,value2,...);

The COALESCE the the function takes a number of arguments and returns the first non-NULL argument. In case all arguments are NULL, the COALESCE function returns NULL.

The following shows some simple examples of using the COALESCE function:

SELECT COALESCE(NULL, 0); -- 0 SELECT COALESCE(NULL, NULL); -- NULL;

MySQL COALESCE function examples

See the following customers the the table in the sample database.

The following query returns the customer name, city, state, and country of all customers in the customers table.

SELECT customerName, city, state, country FROM customers;

As you see, the state column has NULL values because some of this information is not applicable to the country of some customers.

To substitute the NULL value in the result set, you can use the COALESCE function as follows:

SELECT customerName, city, COALESCE(state, 'N/A'), country FROM customers;

In this example, if the value in the state column is NULL, the COALESCE function will substitute it by the N/A string. Otherwise, it returns the value of the state column.

Another typical example of using the COALESCE function is to substitute a value in one column with another when the first one is NULL.

Suppose you have a articles table with the following structure:

CREATE TABLE articles ( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(255) NOT NULL, excerpt TEXT, body TEXT NOT NULL, published_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP );

Let’s insert some data into the articles table.

INSERT INTO articles(title,excerpt,body) VALUES('MySQL COALESCE Tutorial','This tutorial is about MySQL COALESCE function', 'all about COALESCE function'), ('MySQL 8.0 New Features',null, 'The following is a list of new features in MySQL 8.0');

Imagine you have to display articles on an overview page where each article contains the title, expert, and publish date (and also the read more link to the article page). The first task you need to do is to query this data from the articles table:

SELECT id, title, excerpt, published_at FROM articles;

As you see the article with id 2 does not have the excerpt, which is not nice for display.

A typical solution is to get the first number of characters in the body of the article for displaying as the excerpt. This is why the COALESCE function comes into play.

SELECT id, title, COALESCE(excerpt, LEFT(body, 150)), published_at FROM articles;

In this example, if the value in the excerpt column is NULL, the COALESCE the function returns the first 150 characters of the content in the body column.

MySQL COALESCE and CASE expression

Besides using the COALESCE function, you can use the CASE expression to achieve the same effect.

The following query uses the CASE expression to achieve the same result as the example above:

SELECT id, title, (CASE WHEN excerpt IS NULL THEN LEFT(body, 150) ELSE excerpt END) AS excerpt, published_at FROM articles;

In this example, the CASE the expression is more lengthy than using the COALESCE function.

MySQL COALESCE vs. IFNULL

The IFNULL the function takes two arguments and returns the first argument if it is not NULL, otherwise, it returns the second argument.

The IFNULL the function works great with two arguments whereas the COALESCE function works with n arguments. In case the number of arguments is two, both functions are the same.

In this tutorial, you have learned how to use the MySQL COALESCE function to substitute NULL values.

Reactions

Post a Comment

0 Comments

close