MySQL CONCAT() Function
MySQL CONCAT() Function
The CONCAT() function joins two or more expressions end to end into a single string. Unlike the + operator in some languages, MySQL does not add strings with +.
Syntax
CONCAT(str1, str2, ...)
- str1, str2, ...: One or more expressions to combine.
- Return value: A single concatenated string, or
NULLif any argument isNULL.
MySQL automatically converts numeric arguments to their string equivalents. If you pass a single argument, CONCAT() returns that value cast as a string.
Practical Example
Suppose you want to display full customer labels from demo_customers by joining first_name, last_name, and city. Some customers have not provided a city yet.
SELECT
customer_id,
CONCAT(first_name, ' ', last_name) AS full_name,
CONCAT(first_name, ' (', city, ')') AS raw_label,
CONCAT(first_name, ' (', COALESCE(city, 'Unknown'), ')') AS safe_label
FROM demo_customers;
| customer_id | full_name | raw_label | safe_label |
|---|---|---|---|
| 1 | Maya Lin | Maya (Austin) | Maya (Austin) |
| 2 | Liam Chen | NULL | Liam (Unknown) |
| 3 | Nora Diaz | Nora (Madrid) | Nora (Madrid) |
In row 2, city is NULL, so raw_label becomes NULL even though first_name has a value. Wrapping city in COALESCE() preserves the customer name.
Common Mistakes & NULL Behavior
- Strict NULL propagation:
CONCAT()returnsNULLif any input argument isNULL. Contrast that withCONCAT_WS(), which skipsNULLvalues automatically. - Using
+instead of CONCAT(): Writingfirst_name + ' ' + last_namein MySQL triggers numeric addition and evaluates strings as0. - Mixing binary and text values: If any argument is a binary string (
VARBINARY,BLOB),CONCAT()returns a binary string, which may disable case-insensitive comparisons later.
Frequently Asked Questions
Why did CONCAT() erase the entire row output?
One of the columns in that row held NULL. Wrap optional fields in COALESCE(col, '') or IFNULL(col, ''), or use CONCAT_WS().
Can I use the || operator instead of CONCAT() in MySQL?
By default, || means logical OR in MySQL, not string concatenation. It only concatenates strings if PIPES_AS_CONCAT SQL mode is enabled.
How many arguments can CONCAT() accept?
You can pass one or dozens of expressions separated by commas in a single CONCAT() call.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.