Function Reference

TRIM

Removes spaces or the given substring from the beginning and end of a string.

MySQL 8.1
TRIM([[LEADING | TRAILING | BOTH] [remstr] FROM] str)
  • [ ]optional part
  • a | bone of the options
LEADING | TRAILING | BOTH
Side to trim, BOTH (both sides) by default
KeywordOptional
LEADINGTRAILINGBOTH
remstr
Substring to remove, a space by default
VARCHAROptional
str
Source string
VARCHAR

Return value

A string without remstr (spaces by default) on the chosen sides; tabs and line breaks are kept by default

Examples

Spaces removed on both sides

MySQL 8.1
SELECT CONCAT('[', TRIM('  Hello  '), ']') AS result;
result
[Hello]

Leading zeros removed

MySQL 8.1
SELECT TRIM(
		LEADING '0'
		FROM '000123'
	);
TRIM(LEADING '0' FROM '000123')
123

Repeated substring removed

MySQL 8.1
SELECT TRIM(
		'ab'
		FROM 'ababcab'
	);
TRIM('ab' FROM 'ababcab')
c

Details

remstr is removed as a whole and as many times in a row as it occurs. Unlike PostgreSQL, it is a substring, not a set of separate characters.

See also