SyntaxStudy
Sign Up
MySQL String Functions in MySQL
MySQL Beginner 5 min read

String Functions in MySQL

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.

Example
SELECT
    CONCAT(first_name, ' ', last_name) AS full_name,
    UPPER(email) AS email_upper,
    SUBSTRING(phone, 1, 3) AS area_code,
    REPLACE(bio, '<script>', '') AS clean_bio,
    TRIM(username) AS username_clean
FROM users;
Pro Tip

Use CHAR_LENGTH() instead of LENGTH() when working with multi-byte character sets like UTF-8.