Tableau function String

Tableau ASCII to SQL

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)

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

Upper-case A is 65, lower-case b is 98, and the null row stays null.

NameResult
Ann Lee65
bob ray98
Café Nord67
east101
East69
mary-jo o'neil109
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)

SELECT
  CASE
    WHEN length(Code) >= 2 THEN ascii(substr(Code, 2))
    END AS result
FROM input
ORDER BY id

Most codes have a hyphen (45) second. Code X has no second character, so the result is null.

CodeResult
A-12-x45
B-7145
C-301-yz-945
a_195
Xnull
D-4-y45
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

SELECT ascii(substr(Code, 2)) AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  CASE
    WHEN length(Code) >= 2 THEN ascii(substr(Code, 2))
    END AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
A-12-x454545
B-71454545
C-301-yz-9454545
a_1959595
Xnull0null
D-4-y454545
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 ASCII(Name) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

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

BigQuery documentation

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

Does ASCII look at the whole string?

No, only the first character. ASCII('bob ray') is 98, the code of b.

Is ASCII case sensitive?

Yes. Upper-case E is 69 and lower-case e is 101, so rows 4 and 5 of the sample differ. Wrap the field in UPPER() first if case should not matter.

What is the opposite of ASCII?

CHAR: CHAR(65) is A. In DuckDB the pair is ascii() and chr().

Where is this documented?