Tableau function String

Tableau REPLACE to SQL

REPLACE(string, substring, replacement) replaces every occurrence of the substring, read literally and case sensitively. DuckDB's replace() matches. regexp_replace is not a drop-in: there . means any character.

Updated · Checked on the xmetric engine

What is the syntax?

REPLACE(string, substring, replacement)
string
the text.
substring
the literal text to find.
replacement
the text to put in its place.

Returns the string with every substring replaced. Null if an input 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.

How do I remove the dots from each email?

Tableau

REPLACE([Email], '.', '')

SQL (DuckDB)

SELECT replace(Email, '.', '') AS result
FROM input
ORDER BY id

ann@shop.com becomes ann@shopcom.

EmailResult
ann@shop.comann@shopcom
Bob.Ray@Mail.orgBobRay@Mailorg
cafe.nord@shop.frcafenord@shopfr
null
no emailno email
mj_2@shop.commj_2@shopcom
null

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

Where does a straight translation go wrong?

Common mistake

Why does regexp_replace empty the whole email?

In a regex, . matches any character, so every character goes. REPLACE takes the dot literally. Use replace(), or escape it as '\.'.

Tableau

REPLACE([Email], '.', '')

Tempting SQL

SELECT regexp_replace(Email, '.', '', 'g') AS result
FROM input
ORDER BY id

SQL that matches

SELECT replace(Email, '.', '') AS result
FROM input
ORDER BY id
EmailTableauTempting SQLSQL that matches
ann@shop.comann@shopcomann@shopcom
Bob.Ray@Mail.orgBobRay@MailorgBobRay@Mailorg
cafe.nord@shop.frcafenord@shopfrcafenord@shopfr
nullnullnull
no emailno emailno email
mj_2@shop.commj_2@shopcommj_2@shopcom
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 REPLACE(Email, '.', '') AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT REPLACE(Email, '.', '') AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own REPLACE 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 REPLACE?

Is REPLACE case sensitive?

Yes. REPLACE([Name], 'e', 'E') leaves the é and any capital E alone.

What if the substring is empty?

The string comes back unchanged, in Tableau and in DuckDB.

Where is this documented?