MySQL

MySQL CONCAT Function: The Definitive Guide

intro

Let’s learn everything you need to know about the MySQL CONCAT function and become an expert in string concatenation in the database.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

String concatenation is a common method of data aggregation. It involves taking one or more strings and joining them into a single set of data. Given the frequency of that operation, databases provide specific tools to handle it effectively. Enter the MySQL CONCAT function!

In this guide, you will learn what the CONCAT MySQL function is, how it works, and when and how to use it.

Let's dive in!

What Is the CONCAT MySQL Function?

In MySQL, CONCAT is a function that returns the string resulting from concatenating the values passed as arguments. In other words, it is used to combine two or more strings into a single string.

The MySQL CONCAT string function takes multiple string arguments and returns them as a continuous sequence of characters. That is useful for combining the values of multiple columns or expressions into a unified output.

CONCAT in MySQL: Syntax and First Examples

Below is the syntax of the CONCAT MySQL function:

Copy
        
1 CONCAT(str_1, str_2, ..., str_n)

Where str_1, str_2, ..., str_n is a list of string values separated by commas.

The function must be called with at least one argument. Otherwise, it returns the following SQL syntax error:

Copy
        
1 Incorrect parameter count in the call to native function 'CONCAT'

The result of the CONCAT function is a string obtained from concatenating all given arguments. For example, take a look at the following query:

Copy
        
1 SELECT CONCAT("Hello", ", ", "World!");

This will return:

Copy
        
1 "Hello, World!"
Using `CONCAT` in DbVisualizer
Using `CONCAT` in DbVisualizer

When using the MySQL CONCAT string function, you must keep in mind that:

  • If all arguments are nonbinary strings, the result will be a nonbinary string.
  • If the arguments include any binary strings, the result will be a binary string.
  • Numeric arguments will be converted to their equivalent nonbinary string form.
  • If one of the arguments is NULL, the result will be NULL.

Learn more about binary and nonbinary strings in the official documentation.

So, if you use CONCAT with NULL values, the result will always be NULL:

Copy
        
1 SELECT CONCAT("Hello", NULL, "World!");
Note the NULL values
Note the NULL values

MySQL CONCAT String Values: Use Cases

Time to explore the most common MySQL CONCAT string joining examples.

Note: The queries in the following examples will be executed in DbVisualizer, the database client with the highest user satisfaction on the market. Any other database client will do.

Column Concatenation

The most common use case for CONCAT is combining values from multiple columns into a single output.

For example, assume you have a users table with the first_name and last_name columns. Your goal is to combine them into a full name.

You can easily achieve that with the following CONCAT MySQL query:

Copy
        
1 SELECT id, CONCAT(first_name, " ", last_name) AS full_name 2 FROM users;

The result will be something like:

Executing the query in DbVisualizer
Executing the query in DbVisualizer

Note that the full_name column in the result set is the concatenation of first_name and last_name as expected.

Generated Columns

Another common scenario for using CONCAT is when defining SQL generated columns that store concatenated values:

Copy
        
1 ALTER TABLE users 2 ADD full_name VARCHAR(255) 3 AS (CONCAT(first_name, ' ', last_name)) STORED;

The users table now includes a generated column called full_name, which automatically stores the full name by concatenating the first_name and last_name with a space in between.

For more information, read our guide on SQL generated columns.

Advanced Data Concatenation

Sometimes, just concatenating columns is not enough, and you need to add some extra logic. This is especially true when it comes to concatenating values that can be nullable, as CONCAT does not handle NULL values well (Remember: if an argument of CONCAT is NULL, the result will be NULL).

For example, assume your users table also has an optional middle_name column. You want to concatenate first_name, middle_name, and last_name while taking care of nullable middle_name values and extracting only the first character of middle_name. You can achieve that with the following query:

Copy
        
1 SELECT 2 first_name, 3 middle_name, 4 last_name, 5 CONCAT( 6 first_name, 7 ' ', 8 COALESCE(CONCAT(LEFT(middle_name, 1), '.'), ''), 9 last_name 10 ) AS full_name 11 FROM 12 users;

Note the use of the SQL COALESCE function to replace null middle_name values with empty strings, and LEFT to extract the first character of the middle_name:

Execute this query, and you will get results formatted as desired:

Note that the full_name column contain values in the desired format
Note that the full_name column contain values in the desired format

Wonderful! You now know how to use the CONCAT MySQL function in real-world scenarios.

Conclusion

In this article, you learned about the CONCAT function in SQL. You saw how to use it and which use cases it is helpful for. Now you know that CONCAT helps you concatenate string arguments into a single string.

Working with data aggregation queries involving string concatenation becomes easier with a tool that allows you to deal with databases visually. That’s exactly where a database client like DbVisualizer comes in!

In addition to full support for MySQL and other databases, it offers in-line data editing, advanced query optimization, and drag-and-drop query construction capabilities. Download DbVisualizer for free now!

FAQ

Is it possible to use CONCAT with multiple strings in MySQL?

Yes, you can use the CONCAT function in MySQL to concatenate multiple strings. Simply provide the strings as arguments, separated by commas:

Copy
        
1 CONCAT(str_1, str_2, ..., str_n)

What is the difference between the MySQL CONCAT and GROUP_CONCAT functions?

In MySQL, CONCAT combines multiple strings into a single string. Instead, GROUP_CONCAT aggregates values from multiple rows into a single string, separated by a specified delimiter. CONCAT is typically used for individual row operations, whereas GROUP_CONCAT is useful for summarizing data across groups in GROUP BY queries.

What is the difference between the CONCAT and CONCAT_WS functions in MySQL?

The CONCAT function in MySQL joins multiple strings into a single string. For example, CONCAT("Alice", "Bob", "Charlie") becomes "AliceBobCharlie" . In contrast, CONCAT_WS concatenates strings by using a separator. So, CONCAT_WS(", ", "Alice", "Bob", "Charlie") results in "Alice, Bob, Charlie". Also, CONCAT_WS ignores NULL values while CONCAT considers them. In particular, if one of the arguments of the CONCAT function is NULL, the result will be NULL.

Is it possible to use the CONCAT MySQL function with two numbers?

Yes, you can use the CONCAT function in MySQL with two numbers. When numbers are passed to CONCAT, they are implicitly converted to strings and concatenated. For example, CONCAT(123, 456) will return "123456" as a single string.

What are some best practices for the CONCAT function in MySQL?

  • Ensure the inputs are strings or numbers
  • Be aware that NULL values are ignored when CONCAT_WS is in use, but considered when using CONCAT
  • Keep the number of arguments manageable for readability
  • Use CONCAT_WS for aggregation with separators
  • Use GROUP_CONCAT for group concatenation in GROUP BY queries
Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

A Guide To the SQL DECLARE Statement

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SERVER 7 min 2024-12-12
title

SQL Upsert: Inserting a Record If It Does Not Exist

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-12-10
title

A Complete Guide to the MySQL COUNT Function

author Antonello Zanini tags MySQL 9 min 2024-12-09
title

MySQL Nullable Columns: Everything You Need to Know

author Antonello Zanini tags MySQL 6 min 2024-12-03
title

A Complete Guide to the MySQL GROUP_CONCAT Function

author Antonello Zanini tags Data aggregation MySQL 8 min 2024-12-02
title

How and Why to Add an Index in SQL

author Lukas Vileikis tags MySQL POSTGRESQL Search SQL SQL SERVER 8 min 2024-11-28
title

MySQL 9.0 and the New EXPLAIN Features

author Lukas Vileikis tags MySQL SQL 9 min 2024-11-27
title

MySQL CAST: A Guide to the Data Conversion Function

author Antonello Zanini tags MySQL 3 min 2024-11-25
title

List of SQL Reserved Words in All Major Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 16 min 2024-11-20
title

SQL Variable: What It Is and How To Use It

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-11-18

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗