Javatpoint Logo
Javatpoint Logo

SPACE Function in SQL

This string function of SQL adds the given number of spaces between two characters or a set of strings.

Syntax of SPACE String Function

Syntax1: This syntax uses the SPACE function with the column names of the SQL table:

In the syntax, we have to specify the name of those columns on which we want to use SPACE function.

Syntax2: This syntax uses the SPACE function with two or more strings:

Syntax3: This syntax uses the SPACE function with two or more single characters:

Syntax4: We can also use the given syntax to specify the space:

Examples of SPACE String function

Example 1: The following SELECT query shows the 15 space in the result:

Output:

15_Spaces
'               '

Example 2: The following SELECT query adds the 10 spaces after the JavaTpoint Word:

SELECT 'JavaTpoint' + SPACE(10) AS 10_Space;

Output:

'JavaTpoint          '
10

Example 3: The following SELECT query adds the 20 spaces between two single characters:

Output:

20_Spaces
'S                    K'

Example 4: The following SELECT query adds the 15 spaces between the multiple given strings:

Output:

20_Spaces
'New'               'Delhi'                'is'               'the'               'Capital'               'of'               'India'

Example 5: This example uses the SPACE function with the SQL table

In this example, we are going to create a new table that helps in executing the space function with the string columns.

The syntax for creating the new table in the SQL database is as follows:

The following CREATE statement creates the Student_Grade table:

The below INSERT queries insert the records of students with grades and marks in the Student_Grade table:

The following SELECT statement displays the inserted records of the above Student_Grade table:


Roll_No First_Name Last_Name First_City Second_City New_City Hindi_Marks Maths_Marks Grade
10 Aman Sharma Lucknow Chandigarh Ghaziabad 88 95 A2
02 Vishal Sharma Chandigarh Ghaziabad Ghaziabad 95 82 A1
07 Raj Gupta Delhi Ghaziabad Lucknow 91 95 A1
04 Yash Singhania Ghaziabad Delhi Lucknow 85 82 A2
11 Vinay Roy Delhi Kanpur Ghaziabad 95 97 A1
16 Manoj Gupta Ghaziabad Meerut Chandigarh 95 90 B1
19 Ram Gupta Lucknow Ghaziabad Chandigarh 89 95 A2

The following SELECT query uses the SPACE function with the First_Name and Last_Name column of the above Student_Grade table:

This SQL statement adds the 10 spaces between the first name and last name of each student.

Output:

First_Name Last_Name 10_space
Aman Sharma
Aman          Sharma
Vishal Sharma
Vishal          Sharma
Raj Gupta
Raj          Gupta
Yash Singhania
Yash          Singhania
Vinay Roy
Vinay          Roy
Manoj Gupta
Manoj          Gupta
Ram Gupta
Ram          Gupta

The following SELECT query uses the SPACE function with the First_Name and Grade column of those students whose Roll_No is greater than 2 in the above Student_Grade table:

Output:

Roll_No First_Name + SPACE(5) + Grade
07
Raj     A1
04
Yash     A2
11
Vinay     A1
16
Manoj     B1
19
Ram     A2






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA