CONTAINS(string, substring) is true when the substring appears anywhere in the string, and it is case sensitive. DuckDB's contains() matches it exactly. A LIKE '%...%' rewrite looks the same until the substring holds _ or %, which LIKE reads as wildcards.
Updated · Checked on the xmetric engine
What is the syntax?
CONTAINS(string, substring)
string
the text to search.
substring
the text to look for, matched literally and case sensitively.
Returns true if substring occurs in string, false if not, null if either 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.
Which emails are at a shop domain?
Tableau
CONTAINS([Email], 'shop')
SQL (DuckDB)
SELECTcontains(Email, 'shop') AS result
FROM input
ORDERBY id
Rows 1, 3 and 6 contain shop; the missing email stays null.
Email
Result
ann@shop.com
true
Bob.Ray@Mail.org
false
cafe.nord@shop.fr
true
null
no email
false
mj_2@shop.com
true
null
✓ The Tableau formula and the SQL return the same 7 rows.
Where does a straight translation go wrong?
Common mistake
Why does LIKE find an underscore in every code?
In LIKE, _ means any one character, so '%_%' matches every non-empty string. CONTAINS reads _ literally; only code a_1 has one.
Tableau
CONTAINS([Code], '_')
Tempting SQL
SELECT Code LIKE'%_%'AS result
FROM input
ORDERBY id
SQL that matches
SELECTcontains(Code, '_') AS result
FROM input
ORDERBY id
Code
Tableau
Tempting SQL
SQL that matches
A-12-x
false
true
false
B-71
false
true
false
C-301-yz-9
false
true
false
a_1
true
true
true
X
false
true
false
D-4-y
false
true
false
null
null
null
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
Common mistake
Why does ILIKE match more rows?
ILIKE ignores case, CONTAINS doesn't. 'SHOP' is not in ann@shop.com for Tableau.
Tableau
CONTAINS([Email], 'SHOP')
Tempting SQL
SELECT Email ILIKE'%SHOP%'AS result
FROM input
ORDERBY id
SQL that matches
SELECTcontains(Email, 'SHOP') AS result
FROM input
ORDERBY id
Email
Tableau
Tempting SQL
SQL that matches
ann@shop.com
false
true
false
Bob.Ray@Mail.org
false
false
false
cafe.nord@shop.fr
false
true
false
null
null
null
no email
false
false
false
mj_2@shop.com
false
true
false
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.
Snowflake
SELECTCONTAINS(Email, 'shop') AS result
FROM input
ORDERBY id