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
Syntax
trim([LEADING | TRAILING | BOTH] [characters] FROM string) | btrim(string, characters)
Parameters
| Parameter | Description |
|---|---|
string |
The input text. |
characters optional |
The characters to remove; spaces by default. |
Return value
Text.
Examples
SELECT '[' || trim(' hello ') || ']' AS trimmed;
Output
trimmed --------- [hello] (1 row)
Run this on your computer — see “Get Started” for setup.
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
TRIM()· SQLite
Related
LTRIM()· Remove spaces, or characters you list, from the start of a string only.RTRIM()· Remove spaces, or any characters you list, from the end (right side) of a string only.- SQL String Functions · tutorial