# Trim and collapse whitespace

Shape / columns

A variant of [Clean up text](https://meridian.online/operations/clean-up-text).

Remove spaces, line breaks or given characters from the ends of text and collapse runs of whitespace inside it.

Page: <https://meridian.online/operations/clean-up-text/trim-whitespace>

## Status

Runs as a SQL step.

arcform carries it as a `sql:` step today.

## The DuckDB step

trim the ends and collapse runs of whitespace

```sql
CREATE TEMP TABLE raw AS SELECT * FROM (VALUES
  (1, '  Bob   Okafor  '), (2, '**ID-7**'), (3, E'a \n  b')
) t(id, s);
CREATE TEMP TABLE result AS
SELECT id, trim(regexp_replace(s, '\s+', ' ', 'g')) AS collapsed, trim(s, '*') AS unstarred FROM raw;
SELECT * FROM result ORDER BY id;
SELECT CASE WHEN ((SELECT collapsed FROM result WHERE id = 1) = 'Bob Okafor' AND (SELECT unstarred FROM result WHERE id = 2) = 'ID-7' AND (SELECT collapsed FROM result WHERE id = 3) = 'a b') THEN 'as expected' ELSE error('padding was not trimmed, stars were not stripped, or a line break and spaces were not collapsed to one space') END AS verdict;
```

The statement builds its own data.

It needs nothing installed beyond DuckDB itself.

## What other tools call it

Meridian's classification; an independent blind rating agreed on 95.7% of 324 rows (variant, version 3).

- Duckle: [`Standardize`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- Duckle: [`Trim`](https://github.com/slothflowlabs/duckle/blob/f85f8960edfb6efbf4cb89d58a7b32e16df0e867/website/docs/components.html) (read 2026-09-25)
- EasyMorph: [`Sanitize text`](https://help.easymorph.com/doku.php?id=transformations:sanitize) (read 2026-09-27)
- Ibis: [`LStrip`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/strings.py) (read 2026-09-27)
- Ibis: [`RStrip`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/strings.py) (read 2026-09-27)
- Ibis: [`Strip`](https://github.com/ibis-project/ibis/blob/05d2b293344e86cb41c1a426071734f7e270335a/ibis/expr/operations/strings.py) (read 2026-09-27)
- OpenRefine: [`Collapse consecutive whitespace`](https://openrefine.org/docs/manual/cellediting#collapse-consecutive-whitespace) (read 2026-09-27)
- OpenRefine: [`Trim leading and trailing whitespace`](https://openrefine.org/docs/manual/cellediting#trim-leading-and-trailing-whitespace) (read 2026-09-27)
- OpenRefine: [`Trimming`](https://openrefine.org/docs/manual/grelfunctions#trimming) (read 2026-09-27)
- RapidMiner Studio: [`Trim/Strip`](https://docs.rapidminer.com/latest/studio/operators/blending/values/trim_strip.html) (read 2026-09-27)
- Tableau Prep: [`Remove Extra Spaces`](https://help.tableau.com/current/prep/en-us/prep_clean.htm) (read 2026-09-27)
- Tableau Prep: [`Trim Spaces`](https://help.tableau.com/current/prep/en-us/prep_clean.htm) (read 2026-09-27)
- Trifacta: [`Trim quotes`](https://help.alteryx.com/aac/en/trifacta-classic/reference/transformation-reference.html) (read 2026-09-27)
- Trifacta: [`Trim whitespace`](https://help.alteryx.com/aac/en/trifacta-classic/reference/transformation-reference.html) (read 2026-09-27)
