Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

SQL SPACE Function

by suresh

The SQL SPACE function is one of the String Function, which is used to return the string of repeated spaces. The syntax of the SQL Server SPACE is

SPACE(Integer)

SQL SPACE Function Example 1

The String SPACE Function returns the string of repeated spaces. The following query will show you multiple ways to use this function.

-- SPACE in SQL example
DECLARE @Str1 VARCHAR(50), 
        @Str2 VARCHAR(50)
SET @Str1 = 'Learn ' 
SET @Str2 = 'SQL Server'

SELECT @Str1 + SPACE(1) + @Str2 AS Result1 
SELECT @Str1 + SPACE(3) + @Str2 AS Result2 

SELECT 'Learn' + SPACE(5) +'SQL Server'+ SPACE(8) + '2014' AS Result3

OUTPUT

SQL SPACE Function 2

ANALYSIS

Within this space function example query, it will combine the two variables along with SPACE(1) and produce the string result. It means we are assigning a single space between the two string variables and assigned a new name using ALIAS Column.

SELECT @Str1 + SPACE(1) + @Str2 AS Result1

In the next line, We are adding five spaces in between the Learn, SQL Server, and 8 spaces between SQL Server, 2014

SELECT 'Learn' + SPACE(5) +'SQL Server'+ SPACE(8) + '2014' AS Result3

SPACE Function Example 2

The SPACE function also helpful when you are combining (concatenating) multiple columns. In this example, We are going combine the First name and Last Name columns present in the Employe column to get Full name and to get the space. We will use the SPACE function.

For this string space demonstration, We are going to use the below-shown data

SQL SPACE Function 1

SQL CODE

SELECT [FirstName]
      ,[LastName]
      ,[FirstName] + SPACE(5) + [LastName] AS [Full Name]
      ,[DepartmentName]
      ,[Email]
  FROM [SQL Tutorial].[dbo].[Employe]

OUTPUT

SQL SPACE Function 3

TIP: We used this SPACE(5) to get FIVE empty spaces.

SPACE Function Example 3

In this string space function in SQL server example, We are going combine the First name, five spaces, Last Name, one space, string text = ‘is working as’, eight spaces, and Department name columns available in the Employe table.

SELECT [FirstName]
      ,[LastName]
      ,[DepartmentName]
  	  ,[FirstName] + SPACE(5) + [LastName] + SPACE(1) + 
	  'is Working as' + SPACE(8) + [DepartmentName] AS [Description]
      ,[Email]
  FROM [SQL Tutorial].[dbo].[Employe]

OUTPUT

SQL SPACE Function 4

Placed Under: SQL

Trending Posts

SQL CONCAT Function

Types of Functions in Python

Create SSRS Report using Views

C Program to find Area of an Equilateral Triangle

How to Download Informatica

C Program to Remove First Occurrence of a Character in a String

JavaScript getUTCMonth Function

Python Program to Remove Last Occurrence of a Character in a String

Export Data from SQL Server To Excel Using SSIS Import And Export Wizard

What is SSIS

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy