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 defaultKeywordOptionalLEADINGTRAILINGBOTH
- remstr
- Substring to remove, a space by defaultVARCHAROptional
- str
- Source stringVARCHAR
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;Leading zeros removed
MySQL 8.1
SELECT TRIM(
LEADING '0'
FROM '000123'
);Repeated substring removed
MySQL 8.1
SELECT TRIM(
'ab'
FROM 'ababcab'
);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.