Tableau function String

Tableau SPACE to SQL

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
ORDER BY id

Row 7 has neither, so the result is null.

NameCodeResult
Ann LeeA-12-xAnn Lee A-12-x
bob rayB-71bob ray B-71
Café NordC-301-yz-9Café Nord C-301-yz-9
easta_1east a_1
EastXEast X
mary-jo o'neilD-4-ymary-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)

SELECT length(repeat(' ', 3) || Code) AS result
FROM input
ORDER BY id

Three more than the code itself.

CodeResult
A-12-x9
B-717
C-301-yz-913
a_16
X4
D-4-y8
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

SELECT concat_ws(' ', Name, Code) AS result
FROM input
ORDER BY id

SQL that matches

SELECT Name || repeat(' ', 1) || Code AS result
FROM input
ORDER BY id
NameCodeTableauTempting SQLSQL that matches
Ann LeeA-12-xAnn Lee A-12-xAnn Lee A-12-xAnn Lee A-12-x
bob rayB-71bob ray B-71bob ray B-71bob ray B-71
Café NordC-301-yz-9Café Nord C-301-yz-9Café Nord C-301-yz-9Café Nord C-301-yz-9
easta_1east a_1east a_1east a_1
EastXEast XEast XEast X
mary-jo o'neilD-4-ymary-jo o'neil D-4-ymary-jo o'neil D-4-ymary-jo o'neil D-4-y
nullnull

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
ORDER BY id

Snowflake documentation

BigQuery

SELECT CONCAT(Name, REPEAT(' ', 1), Code) AS result
FROM input
ORDER BY id

BigQuery documentation

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

What does SPACE(0) return?

An empty string. repeat(' ', 0) agrees.

Can I pad a value to a fixed width?

Yes: [Code] + SPACE(10 - LEN([Code])). In DuckDB, rpad(Code, 10, ' ') is simpler.

Where is this documented?