Tip of the Day : Example Uses of the SPACE String Function

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - May 22, 2026

Example Uses of the SPACE String Function

The SPACE string function returns a string of repeated spaces.  The syntax of the SPACE string function is as follows:

SPACE ( < integer_expression > )

The < integer_expression > is a positive integer that indicates the number of spaces.  If < integer_expression > is negative, a NULL string is returned.

Here are sample uses of the SPACE string function

Usage #1 : Right-Align Text

DECLARE @Header1 VARCHAR(25) = 'SQL Server Helper, LLC'
DECLARE @Header2 VARCHAR(25) = '1234 1st Street'
DECLARE @Header3 VARCHAR(25) = 'Somewhere City, NY 01001'
PRINT SPACE(50 - LEN(@Header1)) + @Header1
PRINT SPACE(50 - LEN(@Header2)) + @Header2
PRINT SPACE(50 - LEN(@Header3)) + @Header3
 
Output
---------------
                            SQL Server Helper, LLC
                                   1234 1st Street
                          Somewhere City, NY 01001

Usage #2 : Center-Align Text

DECLARE @Header1 VARCHAR(25) = 'SQL Server Helper, LLC'
DECLARE @Header2 VARCHAR(25) = '1234 1st Street'
DECLARE @Header3 VARCHAR(25) = 'Somewhere City, NY 01001'
PRINT SPACE((50 - LEN(@Header1))/2) + @Header1
PRINT SPACE((50 - LEN(@Header2))/2) + @Header2
PRINT SPACE((50 - LEN(@Header3))/2) + @Header3

 
Output
------------
              SQL Server Helper, LLC
                 1234 1st Street
             Somewhere City, NY 01001

Usage #3 : Justify Text

DECLARE @Contents TABLE ( 
    [Title]         VARCHAR(50),
    [PageNumber]    INT
)

INSERT INTO @Contents ( [Title], [PageNumber] )
VALUES ( 'Introduction', 1)

INSERT INTO @Contents ( [Title], [PageNumber] )
VALUES ( 'Table of Contents', 2)

INSERT INTO @Contents ( [Title], [PageNumber] )
VALUES ( 'Index', 100)

SELECT [Title] + SPACE(50 - LEN([Title])) +
       RIGHT(SPACE(5) + CAST([PageNumber] AS VARCHAR(5)), 5) AS [Output]
FROM @Contents
 
Output
------------
Introduction                                          1
Table of Contents                                     2
Index                                               100

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions