TRIM
Removes the given characters (spaces by default) from the start, the end or both ends of a string.
PostgreSQL 17.9
TRIM([LEADING | TRAILING | BOTH] [characters] FROM string)- [ ]optional part
- a | bone of the options
- LEADING | TRAILING | BOTH
- Side to remove the characters from, BOTH by defaultKeywordOptionalLEADINGTRAILINGBOTH
- characters
- Set of characters to remove, a space by defaulttextOptional
- FROM string
- Source stringtext
Return value
A string without the characters from characters on the chosen side
Examples
Removing a character on both sides
PostgreSQL 17.9
SELECT TRIM(
BOTH 'x'
FROM 'xTomx'
);Removing leading zeros
PostgreSQL 17.9
SELECT TRIM(
LEADING '0'
FROM '000123'
);Spaces by default
PostgreSQL 17.9
SELECT '[' || TRIM(' SQL ') || ']' AS trimmed;Details
Characters are removed until a character outside the set is reached.