Tableau function String

Tableau FIND to SQL

FIND(string, substring) returns the 1-based position of the first match, and 0 when there is none: DuckDB's strpos() does exactly that. With a start position, FIND still counts from the start of the whole string; strpos(substr(...)) counts from the cut, so add the offset back.

Updated · Checked on the xmetric engine

What is the syntax?

FIND(string, substring, [start])
string
the text to search.
substring
the text to find.
start
optional 1-based position to start searching from. The result still counts from the first character.

Returns an integer: the 1-based position of substring in string, 0 if not found, 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 first hyphen in each code?

Tableau

FIND([Code], '-')

SQL (DuckDB)

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

Position 2 in most codes; 0 where there is no hyphen.

CodeResult
A-12-x2
B-712
C-301-yz-92
a_10
X0
D-4-y2
null

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

Where does a straight translation go wrong?

Common mistake

Why does strpos(substr(...)) give a smaller position?

substr cuts the string, so strpos counts from the cut. FIND reports 5 for the second hyphen of A-12-x; the cut string says 3. Add start - 1 back when there is a match.

Tableau

FIND([Code], '-', 3)

Tempting SQL

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

SQL that matches

SELECT
  CASE
    WHEN strpos(substr(Code, 3), '-') = 0 THEN 0
    ELSE strpos(substr(Code, 3), '-') + 2
    END AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-x535
B-71000
C-301-yz-9646
a_1000
X000
D-4-y424
nullnullnull

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

How do I search from a start position?

FIND skips the first characters but reports the position in the whole string. Columns 1 to 7 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)1234567
with startFIND([Code], '-', 3)CASE WHEN strpos(substr(Code, 3), '-') = 0 THEN 0 ELSE strpos(substr(Code, 3), '-') + 2 END506004null

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 POSITION('-', Code) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

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

BigQuery documentation

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

What does FIND return when the substring is missing?

Zero, not null. Code a_1 has no hyphen, so FIND([Code], '-') is 0. Null comes only from a null input.

Is FIND case sensitive?

Yes. FIND('Ann', 'a') is 0. Lower both sides to ignore case.

How do I find the second hyphen?

Use FINDNTH, or FIND with a start just past the first hit.

Where is this documented?