String concatenation in MySQL
Mastering String Concatenation in MySQL

Learn the various methods for concatenating strings in MySQL, including CONCAT(), CONCAT_WS(), and the || operator, with practical examples and best practices.
String concatenation is a fundamental operation in database management, allowing you to combine multiple strings into a single, more meaningful string. In MySQL, there are several ways to achieve this, each with its own use cases and advantages. This article will guide you through the most common methods, providing clear examples and insights into when to use each one.
The CONCAT() Function
The CONCAT()
function is the most straightforward way to concatenate strings in MySQL. It takes two or more string arguments and joins them end-to-end. If any argument is NULL
, the entire result of CONCAT()
will be NULL
. This behavior is important to remember when dealing with potentially null data.
SELECT CONCAT('Hello', ' ', 'World!');
-- Result: 'Hello World!'
SELECT CONCAT('First Name: ', 'John', ', Last Name: ', 'Doe');
-- Result: 'First Name: John, Last Name: Doe'
SELECT CONCAT('Value: ', NULL);
-- Result: NULL
Basic usage of the CONCAT() function
NULL
values. If NULL
is a possibility and you want to avoid a NULL
result, you might need to use IFNULL()
or COALESCE()
in conjunction with CONCAT()
.The CONCAT_WS() Function (Concatenate With Separator)
The CONCAT_WS()
function is a more powerful version of CONCAT()
when you need to concatenate strings with a specific separator. The first argument is the separator, and subsequent arguments are the strings to be joined. A key advantage of CONCAT_WS()
is that it skips NULL
values among the strings to be concatenated, only returning NULL
if the separator itself is NULL
.
SELECT CONCAT_WS(', ', 'Doe', 'John');
-- Result: 'Doe, John'
SELECT CONCAT_WS(' - ', 'Apple', 'Banana', 'Cherry');
-- Result: 'Apple - Banana - Cherry'
SELECT CONCAT_WS(' ', 'Mr.', 'John', NULL, 'Doe');
-- Result: 'Mr. John Doe'
SELECT CONCAT_WS(NULL, 'A', 'B');
-- Result: NULL
Examples demonstrating CONCAT_WS() with and without NULL values
flowchart TD A[Start Concatenation Process] B{Choose Function} C[CONCAT()] D[CONCAT_WS()] E{Any NULLs allowed in output?} F{Need a separator?} G[Output: Single String] A --> B B --> C B --> D C --> E E -- Yes --> G E -- No (handle NULLs) --> G D --> F F -- Yes --> G F -- No (use CONCAT) --> G
Decision flow for choosing between CONCAT() and CONCAT_WS()
The || Operator (ANSI SQL Mode)
In standard SQL, the ||
operator is used for string concatenation. However, in MySQL, ||
typically acts as a logical OR operator by default. To enable ||
for string concatenation, you must activate ANSI SQL mode. This is generally not recommended for new development in MySQL as it can lead to confusion and portability issues with other MySQL installations that don't have ANSI mode enabled.
-- By default, this will perform a logical OR operation:
SELECT 'Hello' || 'World';
-- Result: 1 (true, as both are non-empty strings)
-- To enable concatenation with ||:
SET sql_mode = 'PIPES_AS_CONCAT';
SELECT 'Hello' || ' ' || 'World!';
-- Result: 'Hello World!'
-- To revert to default behavior:
SET sql_mode = '';
Using the || operator for concatenation in ANSI SQL mode
PIPES_AS_CONCAT
mode allows the ||
operator for concatenation, it's generally best to stick with CONCAT()
or CONCAT_WS()
for clarity and compatibility across different MySQL environments. Relying on sql_mode
settings can make your queries less portable.Practical Application: Concatenating Address Fields
Let's consider a common scenario: combining address components from a table into a single, readable address string. We'll use CONCAT_WS()
for this, as it handles potential NULL
values gracefully and allows for a consistent separator.
CREATE TABLE Customers (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50),
address_line1 VARCHAR(100),
address_line2 VARCHAR(100),
city VARCHAR(50),
state VARCHAR(50),
zip_code VARCHAR(10)
);
INSERT INTO Customers (first_name, last_name, address_line1, address_line2, city, state, zip_code) VALUES
('John', 'Doe', '123 Main St', NULL, 'Anytown', 'CA', '90210'),
('Jane', 'Smith', '456 Oak Ave', 'Apt 101', 'Otherville', 'NY', '10001'),
('Peter', 'Jones', '789 Pine Ln', NULL, 'Smalltown', NULL, '54321');
SELECT
customer_id,
CONCAT_WS(' ', first_name, last_name) AS full_name,
CONCAT_WS(', ',
address_line1,
address_line2,
CONCAT_WS(' ', city, state, zip_code)
) AS full_address
FROM Customers;
Concatenating address fields using CONCAT_WS()
In the example above, CONCAT_WS()
is used effectively to build both the full_name
and full_address
. Notice how address_line2
being NULL
for some records doesn't break the full_address
string, and how CONCAT_WS()
is nested to combine city
, state
, and zip_code
before joining them with the rest of the address.