Tableau function String

Tableau CONTAINS to SQL

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)

SELECT contains(Email, 'shop') AS result
FROM input
ORDER BY id

Rows 1, 3 and 6 contain shop; the missing email stays null.

EmailResult
ann@shop.comtrue
Bob.Ray@Mail.orgfalse
cafe.nord@shop.frtrue
null
no emailfalse
mj_2@shop.comtrue
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
ORDER BY id

SQL that matches

SELECT contains(Code, '_') AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-xfalsetruefalse
B-71falsetruefalse
C-301-yz-9falsetruefalse
a_1truetruetrue
Xfalsetruefalse
D-4-yfalsetruefalse
nullnullnull

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
ORDER BY id

SQL that matches

SELECT contains(Email, 'SHOP') AS result
FROM input
ORDER BY id
EmailTableauTempting SQLSQL that matches
ann@shop.comfalsetruefalse
Bob.Ray@Mail.orgfalsefalsefalse
cafe.nord@shop.frfalsetruefalse
nullnullnull
no emailfalsefalsefalse
mj_2@shop.comfalsetruefalse
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 CONTAINS(Email, 'shop') AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT STRPOS(Email, 'shop') > 0 AS result
FROM input
ORDER BY id

BigQuery documentation

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

Is CONTAINS case sensitive?

Yes: CONTAINS([Email], 'SHOP') is false on every row. Lower both sides for a case-blind test: CONTAINS(LOWER([Email]), 'shop').

What does CONTAINS return for a null field?

Null, not false. Row 4 has no email, so it returns null and a filter on the result drops it.

Can I test for several substrings at once?

Combine tests with OR, or use REGEXP_MATCH([Email], 'shop|mail'). In DuckDB, regexp_matches(Email, 'shop|mail').

Where is this documented?