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)
SELECTnullif(regexp_extract(Email, '(\w+)@(\w+)', 1), '') AS result
FROM input
ORDERBY id
\w does not match the dot, so Bob.Ray@Mail.org gives Ray.
Email
Result
ann@shop.com
ann
Bob.Ray@Mail.org
Ray
cafe.nord@shop.fr
nord
null
no email
null
mj_2@shop.com
mj_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
SELECTregexp_extract(Email, '(\w+)@(\w+)', 2) AS result
FROM input
ORDERBY id
SQL that matches
SELECTnullif(regexp_extract(Email, '(\w+)@(\w+)', 2), '') AS result
FROM input
ORDERBY id
Email
Tableau
Tempting SQL
SQL that matches
ann@shop.com
shop
shop
shop
Bob.Ray@Mail.org
Mail
Mail
Mail
cafe.nord@shop.fr
shop
shop
shop
null
null
null
no email
null
null
mj_2@shop.com
shop
shop
shop
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 take the second capture group?
The same pattern, index 2: the word after the @. Columns 1 to 7 are the sample rows, by id.