Space Function
The Space string function returns a string with the specified number of space characters (blanks).
Syntax
Space (integer)
This function returns an integer.
Without Space function
The following example shows concatenation of two string values without using a space function.
Example
Declare @FirstName varchar(40)= 'rohatash'
Declare @LastName varchar(40)= 'Kumar'
select @FirstName + @LastName
Now press F5 to execute it.
Output
![Query-without-space-function-in-sql-server.jpg]()
Space Function
To remove the preceding spacing problem between first name and last name, use the space function.
Example
-- Space Function in SQL Server
Declare @FirstName varchar(40)= 'rohatash'
Declare @LastName varchar(40)= 'Kumar'
select @FirstName + space(1)+@LastName
Output
![Query-with-space-function-in-sql-server.jpg]()
Example
select 'Smith' + ',' + SPACE(1) + 'a-cc,captowm' + ',' +
SPACE(1) + 'captowm' + ',' + Space(2) + 'South Africa' as Address
Output
![address-with-space-function-in-sql-server.jpg]()
Space with print statement
Example
-- Space Function with print in SQL Server
Declare @FirstName varchar(40)= 'rohatash'
Declare @LastName varchar(40)= 'Kumar'
Print @FirstName +space(1)+ @LastName
Output
![print-statement-with-space-function-in-sql-server.jpg]()