String Functions in MySQL
MySQL provides various string functions to perform operations such as concatenation, trimming,
substring extraction, case conversion, padding, and searching within strings. These functions help
in text manipulation and formatting.
Function Description Example Output
CONCAT() Joins two or more strings CONCAT('Dr. ', 'Ajmer ', 'Singh') Dr. Ajmer Singh
CONCAT_WS() Joins strings with separator CONCAT_WS('-', '2025', '10', '09') 2025-10-09
LENGTH() Returns length in bytes LENGTH('MySQL') 5
CHAR_LENGTH() Returns character count CHAR_LENGTH('MySQL') 5
UPPER() Converts to uppercase UPPER('mysql') MYSQL
LOWER() Converts to lowercase LOWER('MySQL') mysql
SUBSTRING() Extracts substring SUBSTRING('Database',1,4) Data
LEFT() Leftmost characters LEFT('Ajmer',3) Ajm
RIGHT() Rightmost characters RIGHT('Singh',2) gh
REPLACE() Replaces substring REPLACE('DCRUST','UST','UNI') DCRUNI
INSTR() Finds substring position INSTR('Ajmer Singh','Singh') 7
REVERSE() Reverses string REVERSE('MySQL') LQSyM
LTRIM() Removes left spaces LTRIM(' Hello') Hello
RTRIM() Removes right spaces RTRIM('Hello ') Hello
TRIM() Removes both sides spaces TRIM(' Hello ') Hello
LPAD() Pads on left LPAD('45',5,'0') 00045
RPAD() Pads on right RPAD('45',5,'0') 45000
REPEAT() Repeats string REPEAT('SQL ',3) SQL SQL SQL
SPACE() Returns n spaces CONCAT('Hello',SPACE(3),'World') Hello World
Examples:
1■■ Combining and Formatting
SELECT CONCAT_WS(' ', UPPER(first_name), LOWER(last_name)) AS FullName FROM
employees;
2■■ Extracting and Searching
SELECT SUBSTRING('Deenbandhu Chhotu Ram University', 12, 6); -- Output: Chhotu
3■■ Replacing and Trimming
SELECT TRIM(REPLACE(' DCRUST ', ' ', '')); -- Output: DCRUST
4■■ Padding and Repeating
SELECT LPAD('25', 4, '0'), REPEAT('*', 5); -- Output: 0025 , *****
Category Functions
Case Conversion UPPER(), LOWER()
Concatenation CONCAT(), CONCAT_WS()
Substring SUBSTRING(), LEFT(), RIGHT()
Search INSTR(), LOCATE()
Replace REPLACE()
Trim LTRIM(), RTRIM(), TRIM()
Padding LPAD(), RPAD()
Length LENGTH(), CHAR_LENGTH()
Misc REPEAT(), REVERSE(), SPACE(), FIELD(), ELT()
Prepared by: Dr. Ajmer Singh Associate Professor, DCRUST Murthal