SQL Tutorial SQL Advanced SQL Database SQL References

MySQL SPACE() Function



The MySQL 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.

mysql> SELECT SPACE(5);
Result: '     '

mysql> 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 *, CONCAT(FirstName, SPACE(5), 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

❮ MySQL Functions