Vol. I · A Postgres HandbookTwelve chapters · · 756 queries

The Query & the Schema

A reading-room reference for writing PostgreSQL by hand — selects to window functions, with the schema in plain view.

FrontChaptersSchemaColophon
Chapters · String Functions · Query 003 / 019
EasyString Functions

TRIM, LTRIM, RTRIM: remove whitespace

The query

SQL
SELECT
  TRIM('  hello world  ') AS trimmed,
  LTRIM('  left  ') AS left_trimmed,
  RTRIM('  right  ') AS right_trimmed,
  TRIM(BOTH 'x' FROM 'xxxHelloxxx') AS custom_trim;
Tested against PostgreSQL 16

Note

“TRIM(BOTH/LEADING/TRAILING) can remove specific characters, not just spaces.

← Previous

LENGTH and CHAR_LENGTH

Next →

CONCAT and || operator for string building

The Query & the Schema

Set in Fraunces and Inter Tight. Printed on parchment, in oxblood and ink. No tracking, no accounts, no JavaScript on the answer pages beyond what your browser needs to copy a query.

Compiled for Postgres 16 · MMXXVI