MySQL provides a rich set of string functions that allow you to manipulate, search, and format textual data stored in databases. These functions are widely used for data cleaning, reporting, and transforming user input.
MySQL string functions operate on character-based data types such as CHAR, VARCHAR, and TEXT. Most functions accept one or more string arguments and return a modified string or numeric result.
// Concatenate multiple strings into one
SELECT CONCAT('Hello', ' ', 'World') AS greeting;
// Extract first five characters from a string
SELECT SUBSTRING('Hello World', 1, 5) AS sub_string;
// Count total characters in a string
SELECT LENGTH('Hello World') AS string_length;
// Convert text to uppercase
SELECT UPPER('hello world') AS upper_case;
// Remove leading and trailing spaces
SELECT TRIM(' Hello World ') AS trimmed_string;
// Replace part of a string with new text
SELECT REPLACE('Hello World', 'World', 'MySQL') AS replaced_string;
Each query returns a transformed version of the input string. For example, CONCAT() joins strings together, while SUBSTRING() extracts only a selected portion of text.
You can combine multiple string functions in a single query to build advanced transformations, such as cleaning user input before storing it in a database.