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)
SELECTlength(Name) AS result
FROM input
ORDERBY id
Café Nord is 9 characters.
Name
Result
Ann Lee
7
bob ray
7
Café Nord
9
east
4
East
4
mary-jo o'neil
14
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
SELECTstrlen(Name) AS result
FROM input
ORDERBY id
SQL that matches
SELECTlength(Name) AS result
FROM input
ORDERBY id
Name
Tableau
Tempting SQL
SQL that matches
Ann Lee
7
7
7
bob ray
7
7
7
Café Nord
9
10
9
east
4
4
4
East
4
4
4
mary-jo o'neil
14
14
14
null
null
null
✓ 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
SELECTLENGTH(Name) AS result
FROM input
ORDERBY id