Function Reference

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 default
KeywordOptional
LEADINGTRAILINGBOTH
characters
Set of characters to remove, a space by default
textOptional
FROM string
Source string
text

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'
	);
btrim
Tom

Removing leading zeros

PostgreSQL 17.9
SELECT TRIM(
		LEADING '0'
		FROM '000123'
	);
ltrim
123

Spaces by default

PostgreSQL 17.9
SELECT '[' || TRIM('  SQL  ') || ']' AS trimmed;
trimmed
[SQL]

Details

Characters are removed until a character outside the set is reached.

See also