Tableau function Type conversion

Tableau STR to SQL

STR(expression) turns a number or other value into text, most often to build a label: STR([Qty]) + " units". In DuckDB that is CAST(x AS VARCHAR). Two things differ: DuckDB writes a whole decimal as "3.0" where Tableau writes "3", and concat() skips nulls where Tableau's + returns null.

Updated · Checked on the xmetric engine

What is the syntax?

STR(expression)
expression
a number, a date, a boolean or any other value.

Returns a string. Null if the input is null.

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 is each quantity as text?

Tableau

STR([Qty])

SQL (DuckDB)

SELECT CAST (Qty AS VARCHAR) AS result
FROM input
ORDER BY id

7 becomes "7"; row 6 has no quantity, so it is null.

QtyResult
77
−3-3
00
1212
55
null

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

How do I build a label from a number?

Tableau

STR([Qty]) + ' units'

SQL (DuckDB)

SELECT CAST (Qty AS VARCHAR) || ' units' AS result
FROM input
ORDER BY id

Row 6 has no quantity, so the whole label is null.

QtyResult
77 units
−3-3 units
00 units
1212 units
55 units
null

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

What is each decimal amount as text?

Tableau

STR([Amount])

SQL (DuckDB)

SELECT
  CASE
    WHEN Amount = trunc(Amount) THEN CAST (CAST (Amount AS BIGINT) AS VARCHAR)
    ELSE CAST (Amount AS VARCHAR)
    END AS result
FROM input
ORDER BY id

3.0 is written "3", 2.5 is "2.5".

AmountResult
2.52.5
−9.7-9.7
33
0.10.1
null
1,234,5681234567.891

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

Where does a straight translation go wrong?

Common mistake

Why does SQL write "3.0" where Tableau writes "3"?

DuckDB keeps the decimal point when it casts a whole DOUBLE to text, so row 3 becomes "3.0". Tableau prints "3". Print whole values through an integer cast.

Tableau

STR([Amount])

Tempting SQL

SELECT CAST (Amount AS VARCHAR) AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  CASE
    WHEN Amount = trunc(Amount) THEN CAST (CAST (Amount AS BIGINT) AS VARCHAR)
    ELSE CAST (Amount AS VARCHAR)
    END AS result
FROM input
ORDER BY id
AmountTableauTempting SQLSQL that matches
2.52.52.52.5
−9.7-9.7-9.7-9.7
333.03
0.10.10.10.1
nullnullnull
1,234,5681234567.8911234567.8911234567.891

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

Common mistake

Why does concat() give a label where Tableau gives null?

concat() treats null as an empty string, so row 6 becomes " units". Tableau's + makes the whole string null. Use ||, which keeps the null.

Tableau

STR([Qty]) + ' units'

Tempting SQL

SELECT concat(Qty, ' units') AS result
FROM input
ORDER BY id

SQL that matches

SELECT CAST (Qty AS VARCHAR) || ' units' AS result
FROM input
ORDER BY id
QtyTableauTempting SQLSQL that matches
77 units7 units7 units
−3-3 units-3 units-3 units
00 units0 units0 units
1212 units12 units12 units
55 units5 units5 units
null unitsnull

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 TO_VARCHAR(Qty) || ' units' AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT CAST (Qty AS STRING) || ' units' AS result
FROM input
ORDER BY id

BigQuery documentation

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

Why does my label say "null units" or nothing?

In Tableau STR(null) + " units" is null, so the label is empty. In SQL || behaves the same; concat() does not, it gives " units".

How do I control decimals in the text?

Round first: STR(ROUND([Amount], 1)). For thousands separators or currency, use the field's number format rather than STR.

What does STR do with whole decimals?

Tableau writes 3.0 as "3"; DuckDB's cast writes "3.0". The SQL on this page prints whole values as integers to match.

Where is this documented?