SPACE(n) returns a string of n spaces, usually to glue fields: [Name] + SPACE(1) + [Code]. In DuckDB that is repeat(' ', n) and ||. Don't swap the + chain for concat_ws: it skips nulls, where Tableau's + returns null.
Updated · Checked on the xmetric engine
What is the syntax?
SPACE(number)
number
how many spaces, 0 or more.
Returns a string of number spaces. Null if the number is null.
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.
How do I join name and code with a space?
Tableau
[Name]+SPACE(1) +[Code]
SQL (DuckDB)
SELECT Name ||repeat(' ', 1) || Code AS result
FROM input
ORDERBY id
Row 7 has neither, so the result is null.
Name
Code
Result
Ann Lee
A-12-x
Ann Lee A-12-x
bob ray
B-71
bob ray B-71
Café Nord
C-301-yz-9
Café Nord C-301-yz-9
east
a_1
east a_1
East
X
East X
mary-jo o'neil
D-4-y
mary-jo o'neil D-4-y
null
✓ The Tableau formula and the SQL return the same 7 rows.
How many characters does SPACE(3) add?
Tableau
LEN(SPACE(3) +[Code])
SQL (DuckDB)
SELECTlength(repeat(' ', 3) || Code) AS result
FROM input
ORDERBY id
Three more than the code itself.
Code
Result
A-12-x
9
B-71
7
C-301-yz-9
13
a_1
6
X
4
D-4-y
8
null
✓ The Tableau formula and the SQL return the same 7 rows.
Where does a straight translation go wrong?
Common mistake
Why does concat_ws return a value for the empty row?
concat_ws skips nulls and returns '' when every part is null. Tableau's + returns null as soon as one side is null.
Tableau
[Name]+SPACE(1) +[Code]
Tempting SQL
SELECTconcat_ws(' ', Name, Code) AS result
FROM input
ORDERBY id
SQL that matches
SELECT Name ||repeat(' ', 1) || Code AS result
FROM input
ORDERBY id
Name
Code
Tableau
Tempting SQL
SQL that matches
Ann Lee
A-12-x
Ann Lee A-12-x
Ann Lee A-12-x
Ann Lee A-12-x
bob ray
B-71
bob ray B-71
bob ray B-71
bob ray B-71
Café Nord
C-301-yz-9
Café Nord C-301-yz-9
Café Nord C-301-yz-9
Café Nord C-301-yz-9
east
a_1
east a_1
east a_1
east a_1
East
X
East X
East X
East X
mary-jo o'neil
D-4-y
mary-jo o'neil D-4-y
mary-jo o'neil D-4-y
mary-jo o'neil D-4-y
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.
Snowflake
SELECT Name ||REPEAT(' ', 1) || Code AS result
FROM input
ORDERBY id