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)
SELECTends_with(Code, '-x') AS result
FROM input
ORDERBY id
Only A-12-x.
Code
Result
A-12-x
true
B-71
false
C-301-yz-9
false
a_1
false
X
false
D-4-y
false
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
ORDERBY id
SQL that matches
SELECTends_with(Code, '_1') AS result
FROM input
ORDERBY id
Code
Tableau
Tempting SQL
SQL that matches
A-12-x
false
false
false
B-71
false
true
false
C-301-yz-9
false
false
false
a_1
true
true
true
X
false
false
false
D-4-y
false
false
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
SELECTENDSWITH(Code, '-x') AS result
FROM input
ORDERBY id