Function table / Strings / CONTAINS

CONTAINS: from Tableau to SQL

Tests whether a string contains a substring. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

CONTAINS([label], 'WEST')

Sample data

labelquantityamountflag
cedar cedar 6512.8True
ORCHID-west66-7.6False
maple-east670null
willow north6825.3True
birch-birch694.9False
pine/south70-1.2null
ash719True
nullnullnullnull

The same calculation as SQL

SELECT contains(label,'WEST') AS result FROM input

Runs the example above.

Result

result
False
False
False
False
False
False
False
null

Same rows from the calculation and from the SQL in DuckDB.

Where a naive translation goes wrong

  • Replacing CONTAINS with contains(lower(label),'west') changes the shown values or null handling.

    Naive SQL

    SELECT contains(lower(label),'west') AS result FROM input
    result
    False
    True
    False
    False
    False
    False
    False
    null

    Matches the calculation

    SELECT contains(label,'WEST') AS result FROM input
    result
    False
    False
    False
    False
    False
    False
    False
    null

Signature

CONTAINS(string, string)