SPACE Function in SQLThis string function of SQL adds the given number of spaces between two characters or a set of strings. Syntax of SPACE String FunctionSyntax1: 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 functionExample 1: The following SELECT query shows the 15 space in the result: Output:
Example 2: The following SELECT query adds the 10 spaces after the JavaTpoint Word: SELECT 'JavaTpoint' + SPACE(10) AS 10_Space; Output:
Example 3: The following SELECT query adds the 20 spaces between two single characters: Output:
Example 4: The following SELECT query adds the 15 spaces between the multiple given strings: Output:
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:
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:
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:
Next TopicSUBSTRING Function in SQL |