MySQL string functions let you build, cut, clean and search text directly inside a query. CONCAT() joins values into one string, and SUBSTRING() returns part of a string starting at a position you choose. Together with helpers such as UPPER(), TRIM(), REPLACE() and CHAR_LENGTH(), they cover most text shaping you need before the data reaches your application.
This guide uses MySQL 8.4 LTS syntax, which also works on MySQL 9.x. All examples run against one small table, so you can paste them into the mysql client and compare your output.
Create a table of customers with a few deliberately messy values, such as extra spaces and a missing middle name.
The leading spaces in ' asha' and the trailing space in 'Verma ' are intentional. You will clean them with TRIM() later in this article.
CONCAT(a, b, c, ...) returns the arguments joined end to end. Numbers are converted to strings automatically.
Watch out for NULL. If any argument of CONCAT() is NULL, the whole result is NULL. That is a common cause of rows that mysteriously show an empty name.
Only Rohan's row returns a value here, because the other two rows have a NULL middle name. CONCAT_WS(separator, ...) solves this. The WS stands for "with separator", and it skips NULL arguments instead of poisoning the result.
Prefer CONCAT_WS() whenever some of the pieces may be missing. If you only want to replace NULL with an empty string for one column, wrap it with IFNULL(middle_name, '').
SUBSTRING(str, pos, len) returns len characters starting at position pos. Positions start at 1, not 0. If you omit len, it returns everything from pos to the end. A negative pos counts from the end of the string.
SUBSTR() and MID() are synonyms of SUBSTRING(). For the common cases of "first N" and "last N" characters, LEFT(str, n) and RIGHT(str, n) are shorter and easier to read.
To cut text at a delimiter, combine SUBSTRING_INDEX(str, delim, count) with the position of a character. For example, the part of an email before the @:
A positive count returns everything before the count-th delimiter, and a negative count returns everything after it counted from the right.
Real data often has stray spaces and mixed case. These functions normalise it.
| Function | What it does | Example result |
|---|---|---|
TRIM(str) | Removes leading and trailing spaces | TRIM(' hi ') gives hi |
LTRIM(str), RTRIM(str) | Removes spaces on one side only | LTRIM(' hi') gives hi |
UPPER(str), LOWER(str) | Changes case | LOWER('MeEra') gives meera |
REPLACE(str, from, to) | Replaces every occurrence of a substring | REPLACE('a-b-c', '-', '') gives abc |
LPAD(str, len, pad) | Pads on the left to a fixed length | LPAD('7', 3, '0') gives 007 |
A typical clean-up query normalises emails and capitalises names:
The second expression takes the first letter, upper-cases it, then appends the rest in lower case. It turns ' asha' into Asha.
TRIM() also accepts a form with a character to remove, such as TRIM(BOTH '-' FROM '--x--'), which returns x.
CHAR_LENGTH(str) returns the number of characters, while LENGTH(str) returns the number of bytes. For plain ASCII they match, but in a utf8mb4 column a character such as é takes two bytes and an emoji takes four.
Use CHAR_LENGTH() when you want to validate user-visible length. To find where a substring starts, use LOCATE(substr, str) or INSTR(str, substr). Both return 0 when the text is not found.
For simple tests inside WHERE, LIKE 'prefix%' is usually enough. For complex patterns, MySQL also has REGEXP_LIKE(), REGEXP_SUBSTR() and REGEXP_REPLACE().
Wrapping an indexed column in a function, such as WHERE LOWER(email) = '[email protected]', prevents MySQL from using a normal index on email. Two good fixes exist:
utf8mb4_0900_ai_ci collation is already case-insensitive, so WHERE email = '[email protected]' matches regardless of case.CREATE INDEX idx_email_lower ON customers ((LOWER(email)));, which MySQL 8.0 and later support.Also remember that string functions return values according to the connection and column character set. Mixing collations in one expression can raise an "Illegal mix of collations" error.
At least one argument is NULL. Use CONCAT_WS(), or wrap the nullable column in IFNULL(col, '') or COALESCE(col, '').
No. The first character is at position 1. Position 0 returns an empty string, which surprises developers coming from languages that count from 0.
LENGTH() counts bytes and CHAR_LENGTH() counts characters. They differ for any multi-byte text such as accented letters or emoji.
By default || means logical OR in MySQL, not concatenation. Only with the PIPES_AS_CONCAT SQL mode does it concatenate, so use CONCAT() for portable code.
Use CONCAT() to join values and CONCAT_WS() when some may be NULL. Use SUBSTRING(), LEFT(), RIGHT() and SUBSTRING_INDEX() to extract parts, and remember positions start at 1. Clean data with TRIM(), LOWER(), UPPER() and REPLACE(), measure with CHAR_LENGTH(), and avoid wrapping indexed columns in functions inside WHERE.