T-SQL Tutorial T-SQL Advanced Database Management T-SQL References

T-SQL SPACE() Function



The T-SQL (Transact-SQL) SPACE() function returns a string with a specified number of spaces.

Syntax

SPACE(number)

Parameters

number Required. Specify the number of spaces to be returned.

Return Value

Returns a string with a specified number of spaces.

Example 1:

The example below shows the usage of SPACE() function.

SELECT SPACE(5);
Result: '     '

SELECT SPACE(10);
Result: '          '

Example 2:

Consider a database table called Employee with the following records:

EmpIDFirstNameLastName
1JohnSmith
2MarryKnight
3JoWilliams
4KimFischer
5RameshGupta
6HuangZhang

In the query below, the SPACE() function is used to add specified number of spaces while concatenating the records of column FirstName and column LastName.

SELECT *, (FirstName + SPACE(2) + LastName) AS FullName FROM Employee;

This will produce the result as shown below:

EmpIDFirstNameLastNameFullName
1JohnSmithJohn  Smith
2MarryKnightMarry  Knight
3JoWilliamsJo  Williams
4KimFischerKim  Fischer
5RameshGuptaRamesh  Gupta
6HuangZhangHuang  Zhang

❮ T-SQL Functions