Tableau function String

Tableau REGEXP_EXTRACT_NTH to SQL

REGEXP_EXTRACT_NTH(string, pattern, n) returns capture group n of the first match. DuckDB's regexp_extract(string, pattern, n) takes the group as its third argument. As with REGEXP_EXTRACT, no match is null in Tableau and an empty string in DuckDB.

Updated · Checked on the xmetric engine

What is the syntax?

REGEXP_EXTRACT_NTH(string, pattern, index)
string
the text to search.
pattern
a constant regular expression with capturing groups.
index
the group number, 1 for the first group; 0 for the whole match.

Returns the text of capture group index. Null when nothing matches or the 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.

What is the user part of each email?

Tableau

REGEXP_EXTRACT_NTH([Email], '(\w+)@(\w+)', 1)

SQL (DuckDB)

SELECT
  nullif(regexp_extract(Email, '(\w+)@(\w+)', 1), '') AS result
FROM input
ORDER BY id

\w does not match the dot, so Bob.Ray@Mail.org gives Ray.

EmailResult
ann@shop.comann
Bob.Ray@Mail.orgRay
cafe.nord@shop.frnord
null
no emailnull
mj_2@shop.commj_2
null

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

Where does a straight translation go wrong?

Common mistake

Why is 'no email' an empty string in DuckDB?

Without a match regexp_extract returns '', while Tableau returns null. Wrap it in nullif(..., '').

Tableau

REGEXP_EXTRACT_NTH([Email], '(\w+)@(\w+)', 2)

Tempting SQL

SELECT regexp_extract(Email, '(\w+)@(\w+)', 2) AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  nullif(regexp_extract(Email, '(\w+)@(\w+)', 2), '') AS result
FROM input
ORDER BY id
EmailTableauTempting SQLSQL that matches
ann@shop.comshopshopshop
Bob.Ray@Mail.orgMailMailMail
cafe.nord@shop.frshopshopshop
nullnullnull
no emailnullnull
mj_2@shop.comshopshopshop
nullnullnull

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

How do I take the second capture group?

The same pattern, index 2: the word after the @. Columns 1 to 7 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)1234567
group 2REGEXP_EXTRACT_NTH([Email], '(\w+)@(\w+)', 2)nullif(regexp_extract(Email, '(\w+)@(\w+)', 2), '')shopMailshopnullnullshopnull

On every row, each Tableau formula and its SQL return the same value.

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
  REGEXP_SUBSTR(Email, '(\\w+)@(\\w+)', 1, 1, 'e', 2) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT REGEXP_EXTRACT(Email, r '\w+@(\w+)') AS result
FROM input
ORDER BY id

BigQuery documentation

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

What does group 0 return?

The whole match: ann@shop for the first email. DuckDB uses 0 the same way.

What if the pattern has fewer groups than n?

The calculation fails; check the pattern. DuckDB raises an error too.

Where is this documented?