Tableau function String

Tableau SPLIT to SQL

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)

SELECT split_part(Code, '-', 3) AS result
FROM input
ORDER BY id

B-71 has only two pieces, so the result is empty.

CodeResult
A-12-xx
B-71
C-301-yz-9yz
a_1
X
D-4-yy
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

SELECT string_split(Code, '-') [3] AS result
FROM input
ORDER BY id

SQL that matches

SELECT split_part(Code, '-', 3) AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-xxxx
B-71null
C-301-yz-9yzyzyz
a_1null
Xnull
D-4-yyyy
nullnullnull

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.

CaseTableau, then SQL (DuckDB)1234567
last tokenSPLIT([Code], '-', -1)split_part(Code, '-', -1)x719a_1Xynull

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

SELECT SPLIT_PART(Code, '-', 3) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT
  IF(Code IS NULL, NULL, COALESCE(SPLIT(Code, '-') [SAFE_OFFSET(2)], '')) AS result
FROM input
ORDER BY id

BigQuery documentation

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

What does SPLIT return when there are fewer pieces?

An empty string, not null: SPLIT('B-71', '-', 3) is ''.

What does token 0 do?

Tableau rejects it; start at 1, or -1 for the last piece.

Is the delimiter a regex?

No, it is literal text, in Tableau and in split_part.

Where is this documented?