SPLIT(string, delimiter, n) returns the nth piece between delimiters, counting from 1; a negative n counts from the end. A missing piece is an empty string. DuckDB's split_part() behaves the same, negatives included. Indexing a string_split list returns null for a missing piece instead.
Updated · Checked on the xmetric engine
What is the syntax?
SPLIT(string, delimiter, token_number)
string
the text to split.
delimiter
the literal separator.
token_number
1 for the first piece; -1 for the last, -2 for the one before.
Returns the token as a string; an empty string if there is no such token; null if the string 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.
What is the third piece of each code?
Tableau
SPLIT([Code], '-', 3)
SQL (DuckDB)
SELECTsplit_part(Code, '-', 3) AS result
FROM input
ORDERBY id
B-71 has only two pieces, so the result is empty.
Code
Result
A-12-x
x
B-71
C-301-yz-9
yz
a_1
X
D-4-y
y
null
✓ The Tableau formula and the SQL return the same 7 rows.
Where does a straight translation go wrong?
Common mistake
Why does the list index return null for B-71?
string_split(Code, '-')[3] reads past the end of a two-item list and returns null. SPLIT returns an empty string, which counts as a value.
Tableau
SPLIT([Code], '-', 3)
Tempting SQL
SELECTstring_split(Code, '-') [3] AS result
FROM input
ORDERBY id
SQL that matches
SELECTsplit_part(Code, '-', 3) AS result
FROM input
ORDERBY id
Code
Tableau
Tempting SQL
SQL that matches
A-12-x
x
x
x
B-71
null
C-301-yz-9
yz
yz
yz
a_1
null
X
null
D-4-y
y
y
y
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 take the last piece?
A negative token counts from the end; split_part takes the same -1. Columns 1 to 7 are the sample rows, by id.
Case
Tableau, then SQL (DuckDB)
1
2
3
4
5
6
7
last token
SPLIT([Code], '-', -1)split_part(Code, '-', -1)
x
71
9
a_1
X
y
null
✓ On every row, each Tableau formula and its SQL return the same value.
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
SELECTSPLIT_PART(Code, '-', 3) AS result
FROM input
ORDERBY id