SQL Server SPACE() Function

The SPACE() function in SQL Server generates a string consisting of a specified number of space characters. While seemingly simple, this function is quite useful for string padding, formatting, or creating strings of a precise length.



SPACE(): Definition and Usage

SPACE() is frequently used to create strings filled with spaces. This is commonly needed for tasks such as formatting data (like aligning text columns), padding data to a certain length, or generating strings of a specific size for other operations. The function's output is a string containing only spaces; the number of spaces is determined by the input parameter.

Syntax

Syntax

SPACE(number)
      

Parameter Values

Parameter Description
number The number of spaces to generate. This is required and must be a non-negative integer.

Example

Generating a String of Spaces

This example generates a string containing ten spaces.

Syntax

SELECT SPACE(10);
      
Output

          (Ten spaces will appear here in your query result)
      

**Note:** The output above shows ten spaces, but the formatting here prevents you from directly seeing them. When you run this SQL code in a SQL Server environment, you will see ten spaces displayed as the result. Providing a negative number to `SPACE()` will result in an error.