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)
SELECTCAST (Qty AS VARCHAR) AS result
FROM input
ORDERBY id
7 becomes "7"; row 6 has no quantity, so it is null.
Qty
Result
7
7
−3
-3
0
0
12
12
5
5
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)
SELECTCAST (Qty AS VARCHAR) ||' units'AS result
FROM input
ORDERBY id
Row 6 has no quantity, so the whole label is null.
Qty
Result
7
7 units
−3
-3 units
0
0 units
12
12 units
5
5 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)
SELECTCASEWHEN Amount =trunc(Amount) THENCAST (CAST (Amount AS BIGINT) AS VARCHAR)
ELSECAST (Amount AS VARCHAR)
ENDAS result
FROM input
ORDERBY id
3.0 is written "3", 2.5 is "2.5".
Amount
Result
2.5
2.5
−9.7
-9.7
3
3
0.1
0.1
null
1,234,568
1234567.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
SELECTCAST (Amount AS VARCHAR) AS result
FROM input
ORDERBY id
SQL that matches
SELECTCASEWHEN Amount =trunc(Amount) THENCAST (CAST (Amount AS BIGINT) AS VARCHAR)
ELSECAST (Amount AS VARCHAR)
ENDAS result
FROM input
ORDERBY id
Amount
Tableau
Tempting SQL
SQL that matches
2.5
2.5
2.5
2.5
−9.7
-9.7
-9.7
-9.7
3
3
3.0
3
0.1
0.1
0.1
0.1
null
null
null
1,234,568
1234567.891
1234567.891
1234567.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
SELECTconcat(Qty, ' units') AS result
FROM input
ORDERBY id
SQL that matches
SELECTCAST (Qty AS VARCHAR) ||' units'AS result
FROM input
ORDERBY id
Qty
Tableau
Tempting SQL
SQL that matches
7
7 units
7 units
7 units
−3
-3 units
-3 units
-3 units
0
0 units
0 units
0 units
12
12 units
12 units
12 units
5
5 units
5 units
5 units
null
units
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
SELECTTO_VARCHAR(Qty) ||' units'AS result
FROM input
ORDERBY id