Concatenating Strings in MySQL

String concatenation, the process of joining two or more strings together, is a fundamental operation in SQL, and MySQL is no exception. MySQL offers several methods for concatenating strings, including the use of the CONCAT() function, the CONCAT_WS() function for concatenating strings with a specific separator, and the GROUP_CONCAT() function for concatenating strings from multiple rows. Let's explore these methods through examples and highlight MySQL-specific features.

Using the CONCAT() Function

The CONCAT() function is a straightforward method for concatenating strings in MySQL. It takes two or more string arguments and joins them into a single string. Here's a basic example:


SELECT CONCAT('Hello, ', 'world!') AS greeting;

This query concatenates the strings 'Hello, ' and 'world!', producing the output 'Hello, world!'. The CONCAT() function can also concatenate columns, strings, and the results of other functions:


SELECT CONCAT(first_name, ' ', last_name) AS full_name

FROM employees;

In this example, the first_name and last_name columns are concatenated with a space in between to form a full name.

Adding Seperators & Handling NULL Values with CONCAT_WS()

A unique feature of MySQL's CONCAT() function is its behavior with NULL values: if any argument is NULL, the result is also NULL. To handle NULL values more gracefully, MySQL provides the CONCAT_WS() function. It takes a separator as the first argument and then two or more strings to concatenate. Unlike CONCAT(), CONCAT_WS() treats NULL values as empty strings, ensuring the concatenation continues smoothly:


SELECT CONCAT_WS(' ', first_name, middle_name, last_name) AS full_name

FROM employees;

If middle_name is NULL, CONCAT_WS() will skip it and directly concatenate first_name, a space, and last_name.

CONCAT_WS() is particularly useful when you need to concatenate strings with a specific separator, such as a comma or a hyphen:

SELECT CONCAT_WS(', ', city, state) AS location
FROM addresses;

Of course, if you want to concatenate strings without a seperator you can simply pass an empty string as the first argument:

SELECT CONCAT_WS('', first_name, last_name) AS full_name
FROM employees;

Concatenating Strings from Multiple Rows with GROUP_CONCAT()

MySQL offers the GROUP_CONCAT() function, which concatenates values from multiple rows into a single string, separated by commas or a specified separator. This is particularly useful for aggregating values from a group of rows:


SELECT GROUP_CONCAT(first_name ORDER BY first_name ASC SEPARATOR ', ') AS employee_names

FROM employees

GROUP BY department_id;

This query concatenates all first_name values within each department into a single string, with names ordered alphabetically and separated by commas.

Conclusion

Choosing the right function for string concatenation in MySQL depends on the specific requirements of your operation, such as how you wish to handle NULL values and whether you need to concatenate strings from multiple rows. For basic concatenations, CONCAT() offers simplicity. When dealing with NULL values, CONCAT_WS() provides a more robust solution. And for concatenating strings across multiple rows within a group, GROUP_CONCAT() is uniquely powerful. MySQL's string concatenation functions are versatile tools in the SQL developer's toolkit, enabling efficient and effective data manipulation and presentation.

Turn your SQL into Beautiful Dashboards

Create amazing KPI dashboards directly from your SQL database with Dashase

  • Create charts, tables, and widgets
  • Collaboration - Shared Dashboards
  • AI assisted query generation - GPT-4
  • Supports PostgreSQL, MySQL, SQL Server, and more

MySQL Snippets