MS SQL Server SPACE() Function
Sometimes you may need to add space with your desired text in MS SQL Server. There is a function called SPACE
in MS SQL Server by which you can do this. SPACE
function return space string inserts a string into another string.
Let’s see the function syntax:
SPACE( integer_expression );
In this syntax:
- Parameter integer_expression is a positive integer that indicates the number of spaces.
- Function returns number of spaces according to parameter. If integer_expression is negative, a null string is returned.
The following example uses SPACE
function to space with string.
DECLARE @f varchar(30) = 'Minhajur'; DECLARE @m varchar(30) = 'Rahman'; DECLARE @l varchar(30) = 'Khan'; SELECT @f as first_name, @m as middle_name, @l as last_name, @f+SPACE(1)+@m+SPACE(1)+@l as full_name;
Here is the result set.
first_name | middle_name | last_name | full_name |
---|---|---|---|
Minhajur | Rahman | Khan | Minhajur RahmanKhan |
In the above example SPACE function add spaces between first_name & middle_ and middle_name & last_name.
I have an another post MS SQL Server String Related Functions where you will learn about different string related functions of MS SQL Server.
In this tutorial, I have shown you how to add space character by using SPACE function in MS SQL Server. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page http://www.facebook.com/LearningBigDataAnalytics and stay connected.