Using CONCAT in MySQL to Combine Strings Effectively
Daniel Hayes
Full-Stack Engineer · Leapcell

Key Takeaways
CONCAT()
joins multiple strings or column values into a single result.- If any argument is
NULL
, the entire result becomesNULL
. CONCAT_WS()
allows joining strings with a custom separator and ignoresNULL
values.
MySQL provides the CONCAT()
function to combine two or more strings into a single string. This is especially useful when formatting output, combining column values, or creating custom identifiers. In this article, we'll explore how CONCAT()
works, its syntax, and practical use cases.
What is CONCAT()
?
The CONCAT()
function in MySQL is used to concatenate (combine) multiple strings into one. If any argument is NULL
, the result will be NULL
.
Syntax
CONCAT(string1, string2, ..., stringN)
string1
,string2
, ...stringN
: These are the strings or column values you want to combine.- Returns a single string that is the result of all arguments joined together.
Basic Usage
Example 1: Simple string concatenation
SELECT CONCAT('Hello', ' ', 'World') AS greeting;
Result:
greeting |
---|
Hello World |
Example 2: Concatenating column values
Assume a table users
:
first_name | last_name |
---|---|
John | Doe |
Jane | Smith |
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;
Result:
full_name |
---|
John Doe |
Jane Smith |
Handling NULL Values
If any argument in CONCAT()
is NULL
, the entire result will be NULL
.
Example:
SELECT CONCAT('Name: ', NULL);
Result:
CONCAT('Name: ', NULL) |
---|
NULL |
To avoid this, use IFNULL()
or COALESCE()
:
SELECT CONCAT('Name: ', IFNULL(NULL, 'Unknown')) AS output;
Result:
output |
---|
Name: Unknown |
Using CONCAT_WS()
MySQL also provides a special function called CONCAT_WS()
, where WS
stands for "With Separator".
Syntax
CONCAT_WS(separator, string1, string2, ..., stringN)
Example:
SELECT CONCAT_WS('-', '2025', '04', '22') AS date_formatted;
Result:
date_formatted |
---|
2025-04-22 |
This is useful when you want to insert a consistent separator and ignore NULL
values (which are skipped).
Use Cases
- Creating full names by combining
first_name
andlast_name
- Formatting dates or times into readable formats
- Generating unique identifiers (e.g., order codes)
- Constructing paths or URLs
Conclusion
The CONCAT()
function in MySQL is a simple yet powerful tool for joining strings and column values. Whether you're formatting user data, combining values, or handling conditional display, CONCAT()
and its cousin CONCAT_WS()
can help you create clean and readable output directly from your SQL queries.
FAQs
The entire result will be NULL
. Use IFNULL()
to provide a fallback.
CONCAT_WS()
adds a separator between values and skips NULL
entries.
Yes, for example: CONCAT(first_name, ' ', last_name)
.
We are Leapcell, your top choice for hosting backend projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ