PostgreSQL 18 reference · String Functions

PostgreSQL TRIM() Function

Remove spaces, or characters you list, from the start, end or both ends of a string. btrim() is the function form.

PostgreSQL 18

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

trim([LEADING | TRAILING | BOTH] [characters] FROM string)  |  btrim(string, characters)

Parameters

ParameterDescription
string The input text.
characters optional The characters to remove; spaces by default.

Return value

Text.

Examples

Remove spaces
SELECT '[' || trim('   hello   ') || ']' AS trimmed;

Output

 trimmed
---------
 [hello]
(1 row)

Run this on your computer — see “Get Started” for setup.

Other characters, one side
SELECT trim(BOTH '-=' FROM '--==Title==--') AS title,
       trim(LEADING '0' FROM '000420') AS number_text;

Output

 title | number_text
-------+-------------
 Title | 420
(1 row)

Run this on your computer — see “Get Started” for setup.

In other languages

Was this page helpful?