Tableau function String

Tableau ENDSWITH to SQL

ENDSWITH(string, substring) is true when the string ends with the substring, matched literally and case sensitively. DuckDB's ends_with() gives the same answer. Rewriting it as LIKE '%...' breaks when the substring contains _ or %.

Updated · Checked on the xmetric engine

What is the syntax?

ENDSWITH(string, substring)
string
the text to test.
substring
the ending to look for.

Returns true if string ends with substring, 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 codes end in -x?

Tableau

ENDSWITH([Code], '-x')

SQL (DuckDB)

SELECT ends_with(Code, '-x') AS result
FROM input
ORDER BY id

Only A-12-x.

CodeResult
A-12-xtrue
B-71false
C-301-yz-9false
a_1false
Xfalse
D-4-yfalse
null

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

Where does a straight translation go wrong?

Common mistake

Why does LIKE '%_1' match code B-71?

In LIKE, _ is a wildcard for any character, so '%_1' matches anything ending in 1. ENDSWITH looks for a literal underscore.

Tableau

ENDSWITH([Code], '_1')

Tempting SQL

SELECT Code LIKE '%_1' AS result
FROM input
ORDER BY id

SQL that matches

SELECT ends_with(Code, '_1') AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-xfalsefalsefalse
B-71falsetruefalse
C-301-yz-9falsefalsefalse
a_1truetruetrue
Xfalsefalsefalse
D-4-yfalsefalsefalse
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 ENDSWITH(Code, '-x') AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT ENDS_WITH(Code, '-x') AS result
FROM input
ORDER BY id

BigQuery documentation

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

Is ENDSWITH case sensitive?

Yes. ENDSWITH([Code], 'X') is false for A-12-x. Use ENDSWITH(LOWER([Code]), 'x') to ignore case.

What does ENDSWITH return for a null?

Null. Row 7 has no code, so its result is null, not false.

Where is this documented?