×

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 Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 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 SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

3 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

SQL Except

In SQL, we probably use the JOIN clause to receive the combined result from one or more than one table. But sometimes, we want a result that contains data from...

4 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 SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 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.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 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 Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.