Function Reference

TRIM

Removes the given characters (spaces by default) from the start, the end or both ends of a string.

PostgreSQL 17.5
TRIM([LEADING | TRAILING | BOTH] [characters] FROM string)

Parameters

LEADING | TRAILING | BOTHkeywordoptional
Side to remove the characters from, BOTH by default
Values:LEADINGTRAILINGBOTH
characterstextoptional
Set of characters to remove, a space by default
FROM stringtext
Source string

Return value

A string without the characters from characters on the chosen side

Examples

Removing a character on both sides

PostgreSQL 17.5
SELECT TRIM(
		BOTH 'x'
		FROM 'xTomx'
	);
btrim
Tom

Removing leading zeros

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

Spaces by default

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

Details

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

See also