The Ultimate Guide to MySQL String Concatenation: Ditching '+' for CONCAT() and CONCAT_WS()

Published: 2025-11-22
Author: DP
Views: 9
Category: MySQL
Content
## The Problem: A Common Misconception Many developers, especially those with a background in other programming languages or SQL dialects like SQL Server's T-SQL, might instinctively use the `+` operator to concatenate strings in MySQL. For instance, they might attempt to execute the following SQL statement to update a thumbnail URL: ```sql -- The WRONG way UPDATE lm068.content SET thumbnail = code + '_cover.jpg'; ``` However, in MySQL, this statement will not work as expected. This is because the `+` operator in MySQL is for arithmetic addition. When it encounters strings, it tries to convert them to numbers for the calculation. If a string cannot be converted to a valid number, it's treated as `0`. Consequently, the above operation could result in the `thumbnail` field being updated to `0` or another unexpected numeric value, rather than the concatenated string. --- ## The Correct Solution: The `CONCAT()` Function In MySQL, the standard way to perform string concatenation is by using the `CONCAT()` function. It accepts one or more string arguments and joins them into a single string. The correct SQL statement for the problem above is: ```sql UPDATE wiki.lib00.content SET thumbnail = CONCAT(code, '_cover.jpg'); ``` **How It Works:** If the `code` field has a value of `ABC123`, `CONCAT(code, '_cover.jpg')` will return `ABC123_cover.jpg`, which is exactly what we want. **A Key Consideration:** The `CONCAT()` function has an important behavior: if any of its arguments is `NULL`, the entire function will return `NULL`. For example, `SELECT CONCAT('Hello', NULL, 'World')` results in `NULL`. This is something to be mindful of when dealing with columns that may contain `NULL` values. --- ## A More Robust Alternative: The `CONCAT_WS()` Function To handle potential `NULL` values more gracefully, MySQL provides the `CONCAT_WS()` (Concatenate With Separator) function. This function is particularly useful because it automatically ignores any `NULL` values among its arguments. The first argument to `CONCAT_WS()` is the separator, followed by the strings to be joined. **Syntax:** `CONCAT_WS(separator, string1, string2, ...)` **Example:** Suppose we need to join `first_name` and `last_name` with an underscore, but `last_name` might be `NULL`. ```sql -- Using CONCAT() SELECT CONCAT(first_name, '_', last_name) FROM users; -- If last_name is NULL, the result is also NULL -- Using CONCAT_WS() SELECT CONCAT_WS('_', first_name, last_name) FROM users; -- If last_name is NULL, the result is just the first_name's value, avoiding a complete NULL result ``` `CONCAT_WS()` significantly simplifies the logic for concatenating fields where some might be optional. --- ## Best Practice: Test Before You Update An important piece of advice from **DP@lib00**: Before executing any `UPDATE` operation, especially one involving data transformation and concatenation, always preview and verify your logic with a `SELECT` statement first. This helps you confirm that the logic is correct and prevents irreversible errors across your entire table. You can test your logic like this: ```sql SELECT code, thumbnail AS old_thumbnail, CONCAT(code, '_cover.jpg') AS new_thumbnail FROM wiki.lib00.com.content LIMIT 10; -- Limit the rows for a quick preview ``` By inspecting the output in the `new_thumbnail` column, you can ensure the concatenation logic is behaving exactly as you expect before committing to the `UPDATE` statement. This is a simple but extremely effective data safety measure. --- ## Summary | Method | Purpose | `NULL` Handling | Example | |---|---|---|---| | `+` | Arithmetic Addition | Tries to convert `NULL` to `0` | `SELECT 'a' + 'b'` -> `0` | | `CONCAT()` | String Concatenation | If any argument is `NULL`, the result is `NULL` | `CONCAT('wiki.lib00', '.com')` -> `'wiki.lib00.com'` | | `CONCAT_WS()` | Concatenation with Separator | Ignores `NULL` arguments (except the separator) | `CONCAT_WS('-', 'A', NULL, 'B')` -> `'A-B'` | Mastering the correct use of `CONCAT()` and `CONCAT_WS()` is an essential skill for every MySQL developer. Remember, never use `+` for string concatenation, and always test before you modify data!