Tableau function String

Tableau MID to SQL

MID(string, start, [length]) returns the characters from start (counting from 1), up to length of them, or to the end when length is left out. DuckDB's substring(string, start, length) uses the same 1-based start. Starting at 0, as in SQL Server or Python habits, loses a character.

Updated · Checked on the xmetric engine

What is the syntax?

MID(string, start, [length])
string
the text.
start
the 1-based position of the first character to return.
length
optional; how many characters. Left out, MID returns the rest of the string.

Returns the part of string from position start, at most length characters. Null if an input 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 are characters 2 to 4 of each code?

Tableau

MID([Code], 2, 3)

SQL (DuckDB)

SELECT substring(Code, 2, 3) AS result
FROM input
ORDER BY id

A-12-x gives -12.

CodeResult
A-12-x-12
B-71-71
C-301-yz-9-30
a_1_1
X
D-4-y-4-
null

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

Where does a straight translation go wrong?

Common mistake

Why does starting at 0 return one character fewer?

Position 0 sits before the first character, so substring(Code, 0, 3) covers only two. MID counts from 1; translate the start unchanged.

Tableau

MID([Code], 1, 3)

Tempting SQL

SELECT substring(Code, 0, 3) AS result
FROM input
ORDER BY id

SQL that matches

SELECT substring(Code, 1, 3) AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-xA-1A-A-1
B-71B-7B-B-7
C-301-yz-9C-3C-C-3
a_1a_1a_a_1
XXXX
D-4-yD-4D-D-4
nullnullnull

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

What happens when the length is left out?

MID then runs to the end of the string, like substring with two arguments. Columns 1 to 7 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)1234567
no lengthMID([Code], 3)substring(Code, 3)12-x71301-yz-914-ynull

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 SUBSTR(Code, 2, 3) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT SUBSTR(Code, 2, 3) AS result
FROM input
ORDER BY id

BigQuery documentation

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

What if start is past the end?

An empty string. MID([Code], 9, 3) is empty for short codes; DuckDB's substring agrees.

How do I take the domain of an email?

MID([Email], FIND([Email], '@') + 1). In SQL, substring(Email, strpos(Email, '@') + 1), or split_part(Email, '@', 2).

Where is this documented?