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)
SELECTtrim(' '|| Name ||' ') AS result
FROM input
ORDERBY id
The space inside Ann Lee stays.
Name
Result
Ann Lee
Ann Lee
bob ray
bob ray
Café Nord
Café Nord
east
east
East
East
mary-jo o'neil
mary-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
SELECTreplace(' '|| Name ||' ', ' ', '') AS result
FROM input
ORDERBY id
SQL that matches
SELECTtrim(' '|| Name ||' ') AS result
FROM input
ORDERBY id
Name
Tableau
Tempting SQL
SQL that matches
Ann Lee
Ann Lee
AnnLee
Ann Lee
bob ray
bob ray
bobray
bob ray
Café Nord
Café Nord
CaféNord
Café Nord
east
east
east
east
East
East
East
East
mary-jo o'neil
mary-jo o'neil
mary-joo'neil
mary-jo o'neil
null
null
null
✓ 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.