String functions in MySQL let you manipulate and query text data directly within SQL. They are invaluable for formatting output, cleaning up data, and building dynamic values.
Commonly Used String Functions
- CONCAT(s1, s2, ...): Joins strings together
- SUBSTRING(str, pos, len): Extracts part of a string
- UPPER(str) / LOWER(str): Changes case
- TRIM(str): Removes leading and trailing spaces
- REPLACE(str, from, to): Replaces occurrences of a substring
- LENGTH(str): Returns byte length; CHAR_LENGTH returns character count
- LIKE with % and _: Pattern matching inside WHERE
Practical Example
String functions are often used to normalize data during import or to display formatted output. For example, CONCAT can build full names from separate first and last name columns, and TRIM ensures stray spaces from user input do not cause lookup failures.