SQL TRIM

Removes leading and trailing whitespace — but leaves any whitespace INSIDE the string completely untouched.

What & Why

TRIM(text) strips whitespace from both ends of a string, leaving the interior exactly as it was. This is a genuinely important distinction: TRIM fixes leading/trailing spaces, but does nothing about multiple spaces sitting between words in the middle.

See How It Works

BUSINESS QUESTION

Marketing wants to remove edge padding from campaign labels while proving that TRIM leaves repeated spaces inside a name untouched.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
Watch TRIM fix the edges but preserve internal spacesExample 1 of 4
TRIM(' ' || REPLACE(name, ' ', ' ') || ' ')
idnamepadded_nametrimmed_name
1Spring Launch[ Spring Launch ][Spring Launch]
2Retention Webinar??
3Finance Retargeting??
4Enterprise Search??

TRIM removes Spring Launch's edge padding while the three internal spaces remain.

EXAMPLE QUERY
SELECT
  id,
  name,
  '[' || '  ' || REPLACE(name, ' ', '   ') || '  ' || ']' AS padded_name,
  '[' || TRIM('  ' || REPLACE(name, ' ', '   ') || '  ') || ']' AS trimmed_name
FROM marketing.campaigns
ORDER BY id;
Now You Try

Practice this concept

Marketing wants edge padding removed from campaign labels without collapsing repeated spaces inside the names.

Available schema
marketing

Prefix tables with marketing.table_name.

idnamepadded_nametrimmed_name
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario