×

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available in MySQL Server also.

Syntax:

SPACE (number)

SPACE Function only accepts a single parameter. In that the number denotes the specific number of spaces.

This Function will return a value:

  • If the provided number is a positive integer, then the Function will return a string having the specified number of spaces.
  • As the number of spaces should only be positive, if we give a negative number the function will return NULL.

Space Function is applicable in the below mentioned versions:

  • SQL Server 2005
  • SQL Server 2008
  • SQL Server 2008 R2
  • SQL Server 2012
  • SQL Server 2014
  • SQL Server 2016
  • SQL Server 2017

Let’s now look into various examples which describes the functionality of the SPACE Function.

Example1:

SELECT SPACE (20) AS SOLUTION;

Output:

Solution
------------------------------------------|
					
------------------------------------------|

In the above we are resulted with 20 spaces as the solution.

Example2:

With this example we can understand the functionality of the SPACE Function much better.

SELECT ‘Hello’ + SPACE(15) + ‘World!’ AS Solution;

Output:

Solution
---------------------------
Hello               World!
---------------------------

As we mentioned a SPACE of 15 characters in the middle of “hello” and “world!”, we are given the same in the output as Solution.

Example3:

When we give a negative value as an argument to the SPACE Function, it returns a NULL value.

SELECT SPACE ( -5) AS Solution;

Output:


Solution
--------------
NULL
--------------

So, as the argument given is a negative integer we are produced with the NULL Value.

Example4:

Consider a Student Table having ID, FirstName, LastName, and gender as the Columns/Fields of the table.

To print all the student names with specified number of spaces in between the FirstName and LastName, we will write the below query.

SELECT FirstName + SPACE(7) + LastName AS ‘Student Name’ FROM Student;

Output:

Student Name
----------------------------
Sandeep       Jain
Kiran       Priya
Alia       Bhatt
Kaira       Adwani
----------------------------

The main Advantage of using SPACE Function is that we can get to know the number of spaces present between two strings.

Consider the below query

select ‘Data’ + ‘    ‘ + ‘Structures’ ;

In here we could not guess the number of spaces in between the two strings. At random we can guess the number as 3 or 4 spaces.

If we write the same query using SPACE Function,

select ‘Data’ + SPACE (3) + ‘Structures’ ;

we can easily determine the number of spaces in between the given two strings.

Example5:

We can also give a variable as the argument into the SPACE() Function. So, we should declare the value of the variable before giving it into the Function.

DECLARE @size_of_space int
SET @size_of_space = 6
select ‘Six Space’ + SPACE(@size_of_space) + ‘Away!’ as Solution;

Output:
Solution

Solution
-------------------------
Six Space      Away!
-------------------------

Related Topics

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

SQL SET Keyword

This article will provide you a good understanding of the Set keyword in Structured Query Language. What is the SET keyword? The SET keyword is used to specify values for the variables....

3 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.