Tableau function Type conversion

Tableau FLOAT to SQL

FLOAT(expression) converts a number, a string or a boolean to a floating-point number: FLOAT(3) is 3.0, true is 1 and a string that isn't a number gives null. The DuckDB match is TRY_CAST(x AS DOUBLE). Not CAST(x AS FLOAT): in DuckDB FLOAT is a 4-byte number with about 7 significant digits, so 1234567.891 comes back as 1234567.875.

Updated · Checked on the xmetric engine

What is the syntax?

FLOAT(expression)
expression
a number, a numeric string such as "2.7", or a boolean.

Returns a floating-point number (double precision). Null if the input is null or a string that isn't a number.

What does it return, and what is the SQL?

Each example runs on the same 6 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

What number does each code hold?

Tableau

FLOAT([Code])

SQL (DuckDB)

SELECT TRY_CAST(Code AS DOUBLE) AS result
FROM input
ORDER BY id

"2.7" becomes 2.7 and "1234567.891" keeps every digit. "abc" and the missing code return null.

CodeResult
4242
2.72.7
-2.5−2.5
abcnull
1234567.8911,234,568
null

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

How do I turn a flag into 1 and 0?

Tableau

FLOAT([Flag])

SQL (DuckDB)

SELECT CAST (Flag AS DOUBLE) AS result
FROM input
ORDER BY id

true becomes 1, false 0, and the unknown flag on row 3 null.

FlagResult
true1
false0
null
true1
false0
true1

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

Where does a straight translation go wrong?

Common mistake

Why does CAST(x AS FLOAT) change the digits?

DuckDB's FLOAT is single precision (4 bytes), so "1234567.891" comes back as 1234567.875 and "2.7" as 2.7000000477. Tableau's FLOAT is a double. Cast to DOUBLE.

Tableau

FLOAT([Code])

Tempting SQL

SELECT TRY_CAST(Code AS FLOAT) AS result
FROM input
ORDER BY id

SQL that matches

SELECT TRY_CAST(Code AS DOUBLE) AS result
FROM input
ORDER BY id
CodeTableauTempting SQLSQL that matches
42424242
2.72.72.72.7
-2.5−2.5−2.5−2.5
abcnullnullnull
1234567.8911,234,5681,234,5681,234,568
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 TRY_TO_DOUBLE(Code) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT SAFE_CAST(Code AS FLOAT64) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own FLOAT formula return?

Edit the formula and run it on the 6 sample rows. Fields: [id], [Amount], [Qty], [Code], [Flag].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about FLOAT?

What does FLOAT do with text that isn't a number?

Returns null: row 4's code is "abc". DuckDB's CAST raises an error there, which is why the SQL uses TRY_CAST.

What does FLOAT return for a boolean?

1 for true, 0 for false, null for unknown. In SQL, CAST(Flag AS DOUBLE).

Do I need FLOAT to avoid integer division?

Not in Tableau or DuckDB: / already returns a decimal for two integers (7 / 2 is 3.5). PostgreSQL and SQL Server are the ones that cut integer division.

Where is this documented?