Tableau function String

Tableau LEN to SQL

LEN(string) returns the number of characters. DuckDB's length() counts characters the same way. strlen() counts bytes, so any accented letter (é is two bytes in UTF-8) makes it longer.

Updated · Checked on the xmetric engine

What is the syntax?

LEN(string)
string
the text to measure.

Returns an integer: the number of characters. 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.

How long is each name?

Tableau

LEN([Name])

SQL (DuckDB)

SELECT length(Name) AS result
FROM input
ORDER BY id

Café Nord is 9 characters.

NameResult
Ann Lee7
bob ray7
Café Nord9
east4
East4
mary-jo o'neil14
null

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

Where does a straight translation go wrong?

Common mistake

Why does strlen make Café Nord 10 long?

strlen counts bytes, and é takes two bytes in UTF-8. LEN counts characters. Use length().

Tableau

LEN([Name])

Tempting SQL

SELECT strlen(Name) AS result
FROM input
ORDER BY id

SQL that matches

SELECT length(Name) AS result
FROM input
ORDER BY id
NameTableauTempting SQLSQL that matches
Ann Lee777
bob ray777
Café Nord9109
east444
East444
mary-jo o'neil141414
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 LENGTH(Name) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT CHAR_LENGTH(Name) AS result
FROM input
ORDER BY id

BigQuery documentation

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

Does LEN count spaces?

Yes, every space counts, including the one in Ann Lee (7).

What does LEN return for a null?

Null. Row 7 has no name.

How do I find values longer than 8 characters?

LEN([Name]) > 8; in SQL, length(Name) > 8.

Where is this documented?