SPACE

Return a string consisting of n space characters.

Category: stringReturns: VARCHARDialect: Standard

Syntax

SPACE(n)

Description

## Overview Returns a string consisting of the specified number of ASCII space (U+0020) characters. SPACE is a convenience wrapper over REPEAT(' ', n) that reads more clearly at the call site, particularly when padding output for aligned display or building indented text. SPACE(n) and REPEAT(' ', n) produce identical results. Choose whichever name fits the surrounding code. ## Behavior - Returns NULL when the input is NULL. - If `n` is 0 or negative, returns an empty string. - Produces only ASCII space characters (U+0020). For tabs or other whitespace, use REPEAT with the desired character. - The result length in characters and bytes is exactly `n` (ASCII space is always 1 byte). - No upper bound is enforced on `n`, but very large values consume memory proportional to the output. ## Compatibility - Widely supported across SQL dialects with matching semantics. - Equivalent to REPEAT(' ', n).

Parameters

NameTypeDescription
nSpecifies the number of space characters to generate. Zero or negative values yield an empty string.

Examples

-- Five spaces
SELECT SPACE(5);  -- '     '
-- Zero spaces
SELECT SPACE(0);  -- ''
-- Negative input returns empty string
SELECT SPACE(-3);  -- ''
-- NULL propagates
SELECT SPACE(CAST(NULL AS INT));  -- NULL
-- Use in concatenation to build aligned output
SELECT 'A' || SPACE(3) || 'B';  -- 'A   B'
-- Indent a display label by a dynamic depth
SELECT SPACE(depth * 2) || label AS indented_label
FROM ops.analytics.tree_nodes;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →