ASCII(string) returns the numeric code of the string's first character: ASCII('Ann') is 65. DuckDB has the same function, ascii(), and both return Unicode code points, so é is 233. The difference is an empty string: the engine returns null, DuckDB returns 0.
Updated · Checked on the xmetric engine
What is the syntax?
ASCII(string)
string
a string or string field.
Returns an integer: the code point of the first character. Null for a null string.
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 is the code of the first letter of each name?
Tableau
ASCII([Name])
SQL (DuckDB)
SELECTascii(Name) AS result
FROM input
ORDERBY id
Upper-case A is 65, lower-case b is 98, and the null row stays null.
Name
Result
Ann Lee
65
bob ray
98
Café Nord
67
east
101
East
69
mary-jo o'neil
109
null
✓ The Tableau formula and the SQL return the same 7 rows.
What is the code of the second character of a code?
Tableau
ASCII(MID([Code], 2))
SQL (DuckDB)
SELECTCASEWHENlength(Code) >=2THENascii(substr(Code, 2))
ENDAS result
FROM input
ORDERBY id
Most codes have a hyphen (45) second. Code X has no second character, so the result is null.
Code
Result
A-12-x
45
B-71
45
C-301-yz-9
45
a_1
95
X
null
D-4-y
45
null
✓ The Tableau formula and the SQL return the same 7 rows.
Where does a straight translation go wrong?
Common mistake
Why does ascii() return 0 where Tableau returns null?
MID('X', 2) is an empty string. The engine's ASCII returns null for it; DuckDB's ascii('') returns 0, a real code. Guard the empty case with a length check.
Tableau
ASCII(MID([Code], 2))
Tempting SQL
SELECTascii(substr(Code, 2)) AS result
FROM input
ORDERBY id
SQL that matches
SELECTCASEWHENlength(Code) >=2THENascii(substr(Code, 2))
ENDAS result
FROM input
ORDERBY id
Code
Tableau
Tempting SQL
SQL that matches
A-12-x
45
45
45
B-71
45
45
45
C-301-yz-9
45
45
45
a_1
95
95
95
X
null
0
null
D-4-y
45
45
45
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.