Reference guide for MySQL string functions: CONCAT, SUBSTRING, TRIM, REPLACE, UPPER, LOWER, LENGTH, LIKE, REGEXP, FORMAT, and common string manipulation patterns.
Use this file to discover all available pages before exploring further.
VillageSQL is a drop-in replacement for MySQL with extensions.
All examples in this guide work on VillageSQL. Install Now →
MySQL’s string functions cover splitting, joining, searching, formatting, and comparing text. Most follow the same conventions: 1-based indexing, NULL propagation (any NULL argument returns NULL), and case-insensitive comparisons by default.
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;-- CONCAT_WS: join with a separator, skipping NULLsSELECT CONCAT_WS(', ', city, state, country) AS address FROM locations;
CONCAT with a NULL argument returns NULL. CONCAT_WS skips NULL values but keeps the separator for non-NULL ones.SUBSTRING / SUBSTR / MID — extract part of a string:
-- SUBSTRING(string, start, length) — 1-based indexingSELECT SUBSTRING('Hello, World!', 1, 5); -- 'Hello'SELECT SUBSTRING('Hello, World!', 8); -- 'World!' (to end of string)SELECT SUBSTRING('Hello, World!', -6); -- 'World!' (from the right)
SUBSTRING_INDEX — split on a delimiter:
-- Get everything before the second commaSELECT SUBSTRING_INDEX('a,b,c,d', ',', 2); -- 'a,b'-- Get everything after the last dot (domain from email)SELECT SUBSTRING_INDEX('user@example.com', '@', -1); -- 'example.com'
-- % matches zero or more characters, _ matches exactly oneSELECT * FROM products WHERE name LIKE 'Widget%'; -- starts with WidgetSELECT * FROM products WHERE name LIKE '%Pro%'; -- contains ProSELECT * FROM products WHERE code LIKE 'SKU-___-2024'; -- three chars in middle
REGEXP / RLIKE — regular expression matching:
SELECT * FROM users WHERE email REGEXP '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$';SELECT * FROM products WHERE name REGEXP 'Pro|Plus|Max';
REGEXP uses POSIX regex syntax. Use REGEXP_LIKE() (MySQL 8.0+) for the function form.
By default, no — string comparisons use the column’s collation, and most common collations are case-insensitive (e.g., utf8mb4_general_ci, where ci = case-insensitive). For case-sensitive comparisons, use a _cs collation or the BINARY operator:
SELECT * FROM users WHERE BINARY username = 'Alice';-- or: WHERE username COLLATE utf8mb4_bin = 'Alice'
What’s the difference between CHAR_LENGTH and LENGTH?
LENGTH returns byte count; CHAR_LENGTH returns character count. For ASCII strings they’re the same. For UTF-8 strings with multibyte characters (accented letters, emoji, CJK), they differ. Use CHAR_LENGTH for user-visible string length limits.