Tableau function String

Tableau FINDNTH to SQL

FINDNTH(string, substring, n) returns the 1-based position of the nth occurrence, or 0 when there are fewer than n. DuckDB has no FINDNTH: split on the substring, rejoin the first n pieces, and the position is that length plus 1. Guard the case with fewer pieces, or the SQL invents a position past the end.

Updated · Checked on the xmetric engine

What is the syntax?

FINDNTH(string, substring, occurrence)
string
the text to search.
substring
the text to find.
occurrence
which match to return, counting from 1.

Returns an integer: the 1-based position of that occurrence, 0 if there are fewer, 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.

Where is the second hyphen in each code?

Tableau

FINDNTH([Code], '-', 2)

SQL (DuckDB)

SELECT
  CASE
    WHEN len(string_split(Code, '-')) > 2 THEN length(array_to_string(list_slice(string_split(Code, '-'), 1, 2), '-')) + 1
    WHEN Code IS NOT NULL THEN 0
    END AS result
FROM input
ORDER BY id

A-12-x has its second hyphen at 5; B-71 has only one, so the result is 0.

CodeResult
A-12-x5
B-710
C-301-yz-96
a_10
X0
D-4-y4
null

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

Where does a straight translation go wrong?

Common mistake

Why does the split SQL return a position for B-71?

With one hyphen, the first two pieces are the whole string, and its length plus 1 points past the end. Tableau returns 0. Check that there are more than n pieces first.

Tableau

FINDNTH([Code], '-', 2)

Tempting SQL

SELECT
  length(array_to_string(list_slice(string_split(Code, '-'), 1, 2), '-'))
    + 1 AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  CASE
    WHEN len(string_split(Code, '-')) > 2 THEN length(array_to_string(list_slice(string_split(Code, '-'), 1, 2), '-')) + 1
    WHEN Code IS NOT NULL THEN 0
    END AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-x555
B-71050
C-301-yz-9666
a_1040
X020
D-4-y444
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 REGEXP_INSTR(Code, '-', 1, 2) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT INSTR(Code, '-', 1, 2) AS result
FROM input
ORDER BY id

BigQuery documentation

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

Is FINDNTH with 1 the same as FIND?

Yes. FINDNTH([Code], '-', 1) equals FIND([Code], '-').

Does FINDNTH work on every data source?

No. Tableau documents FINDNTH as unavailable for some live connections; it works on extracts and files.

Is there a shorter SQL in Snowflake or BigQuery?

Yes. BigQuery's INSTR(Code, '-', 1, 2) and Snowflake's REGEXP_INSTR(Code, '-', 1, 2) take the occurrence directly.

Where is this documented?