Skip to main content

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.

Combining and Splitting

CONCAT — join strings together:
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_INDEX — split on a delimiter:

Searching Within Strings

LOCATE / POSITION / INSTR — find a substring:
LIKE — pattern matching with wildcards:
REGEXP / RLIKE — regular expression matching:
REGEXP uses POSIX regex syntax. Use REGEXP_LIKE() (MySQL 8.0+) for the function form.

Modifying Strings

REPLACE — replace all occurrences of a substring:
TRIM / LTRIM / RTRIM — remove whitespace or specific characters:
UPPER / LOWER — change case:
LPAD / RPAD — pad to a fixed length:
REPEAT — repeat a string N times:
REVERSE — reverse a string:

Length and Comparison

MySQL has two length functions that produce different results for multibyte characters:
Always use CHAR_LENGTH when you care about the number of characters, not bytes. STRCMP — compare two strings, returns -1, 0, or 1:

Formatting

FORMAT — format a number with thousands separators:
LEFT / RIGHT — take N characters from start or end:

Common Patterns

Extract the domain from an email address:
Truncate long strings for display:
Normalize whitespace:

Frequently Asked Questions

Are MySQL string comparisons case-sensitive?

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:

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.

Troubleshooting

See also