How to use SPACE in Sql Server

06.21.2022

Intro

SQL Server provides the SPACE function to generate a series of spaces. In this article, we will learn how to use SPACE in SQL Server.

The Syntax

The basic syntax of a SPACE is as follows:

SELECT SPACE(n);
  • n is the number of spaces to create.

Getting Setup

For this, we will be using docker. This is recommended for more than just using SQL Server. To find how to install docker go here: https://docs.docker.com/engine/install/

Now create a file called docker-compose.yml and add the following.

version: "3.9"
services:
  db:
    image: "mcr.microsoft.com/mssql/server"
    ports: 
      - 1433:1433
    environment:
        SA_PASSWORD: "Your_password123"
        ACCEPT_EULA: "Y"

Open a terminal and go to the folder the file is located. Then run the following.

docker-compose up

If you are looking for another good reference, you can check here: https://docs.docker.com/samples/aspnet-mssql-compose/.

A Simple Example

Space is a simply function to generates a number of spaces requests. Here is the example.

select space(10) as res;
res

We can also use the space to add spaces between strings and names.

select concat("Jane", space(5), "Doe") as res;
res
Jane Doe