Tableau function String

Tableau TRIM to SQL

TRIM(string) removes spaces from both ends and leaves the spaces between words alone. DuckDB's trim() is the same. replace(x, ' ', '') is a different operation: it glues the words together.

Updated · Checked on the xmetric engine

What is the syntax?

TRIM(string)
string
the text.

Returns the string without leading and trailing spaces. Null if the string is null.

What does it return, and what is the SQL?

Each example runs on the same 7 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

What is left after trimming both ends?

Tableau

TRIM('  ' + [Name] + '  ')

SQL (DuckDB)

SELECT trim('  ' || Name || '  ') AS result
FROM input
ORDER BY id

The space inside Ann Lee stays.

NameResult
Ann LeeAnn Lee
bob raybob ray
Café NordCafé Nord
easteast
EastEast
mary-jo o'neilmary-jo o'neil
null

The Tableau formula and the SQL return the same 7 rows.

Where does a straight translation go wrong?

Common mistake

Why does replace(x, ' ', '') join the words?

It removes every space, including the ones inside the value: Ann Lee becomes AnnLee. TRIM only touches the ends.

Tableau

TRIM('  ' + [Name] + '  ')

Tempting SQL

SELECT replace('  ' || Name || '  ', ' ', '') AS result
FROM input
ORDER BY id

SQL that matches

SELECT trim('  ' || Name || '  ') AS result
FROM input
ORDER BY id
NameTableauTempting SQLSQL that matches
Ann LeeAnn LeeAnnLeeAnn Lee
bob raybob raybobraybob ray
Café NordCafé NordCaféNordCafé Nord
easteasteasteast
EastEastEastEast
mary-jo o'neilmary-jo o'neilmary-joo'neilmary-jo o'neil
nullnullnull

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

How do I write it in Snowflake or BigQuery?

The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.

Snowflake

SELECT TRIM(Name) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT TRIM(Name) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own TRIM formula return?

Edit the formula and run it on the 7 sample rows. Fields: [id], [Name], [Code], [Email], [Num].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about TRIM?

Why are the spaces added inside the formula here?

The sample loader strips spaces around CSV cells, so the examples pad the name in the formula to show what TRIM removes.

Does TRIM turn an all-space string into null?

No, into an empty string. Use IF TRIM([x]) = '' THEN NULL ELSE [x] END to treat blanks as missing; in SQL, nullif(trim(x), '').

Where is this documented?