intro
Let’s learn everything you need to know about the MySQL CONCAT function and become an expert in string concatenation in the database.
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:
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:
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:
1
SELECT CONCAT("Hello", ", ", "World!");
This will return:
1
"Hello, World!"
When using the MySQL CONCAT
string function, you must keep in mind that:
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
:
1
SELECT CONCAT("Hello", NULL, "World!");
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:
1
SELECT id, CONCAT(first_name, " ", last_name) AS full_name
2
FROM users;
The result will be something like:
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:
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:
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:
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:
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.