Open In App

SPACE() function in SQL Server

SPACE() :
This function in SQL Server helps to return a string that has a specified number of spaces. This function is also available in MYSQL with the same name.

Syntax :



SPACE(number)

Parameters :
This function accepts only one parameter.

Returns :



Applicable in the following versions :

Example-1 :
Basic working of SPACE() function, the function will return 5 blank spaces.

SELECT SPACE(5) 
AS Result;

Output :
The function will return 5 blank spaces.

Result
‘     ‘

Example-2 :
Concatenating 2 or more strings using SPACE() function.

Example-3 :
If the user enters a negative number as an argument, then the function will return NULL.

SELECT SPACE(-20) 
AS Result;

Output :

Result
NULL

Example-4 :
Working of SPACE() function with variable, we use variable along with SPACE() function.

DECLARE @space_Size int
SET @space_Size = 7
SELECT 'KeepChasing' + SPACE(@space_size) + 'YourDreams' 
AS Result;

Output :

Result
KeepChasing       Your Dreams
Article Tags :
SQL