Logo 
Search:

SQL Server Articles

Submit Article
Home » Articles » SQL Server » String FunctionsRSS Feeds

SPACE Function

Posted By: Sarita Patel     Category: SQL Server     Views: 3185

This article explains use of SPACE function of sql server with examples.

SPACE function is used to add spaces to a string. 


Syntax of SPACE Function :

SPACE(integer_expression)

integer_expression can be any positive number to indicate number of spaces to add . If you specify negative number then it returns NULL.  

Return type of SPACE function is char.


Examples of SPACE Function :

Example 1 : Use of SPACE function in select clause

SELECT 'Syntax' + SPACE(3) + 'Example' 

Output
Syntax   Example

Above example returns string with 3 spaces added between 'Syntax' and 'Example' words.


Example 2 : Use of SPACE function to display field value of table.

SELECT ContactName, Address + ',' + SPACE(1) + Region + ',' + Space(1) + City + ',' + Space(2) + Country AS Address
FROM   Customers

Output
ContactName             Address
André Fonseca         Av. Brasil, 442, SP, Campinas,  Brazil
Howard Snyder         2732 Baker Blvd., OR, Eugene,  USA 
Manuel Pereira           5ª Ave. Los Palos Grandes, DF, Caracas,  Venezuela
Mario Pontes             Rua do Paço, 67, RJ, Rio de Janeiro,  Brazil
Yoshi Latimer            City Center Plaza 516 Main St., OR, Elgin,  USA

Above example displays address of customer with spaces.
  
Share: 

 
 
 

Didn't find what you were looking for? Find more on SPACE Function Or get search suggestion and latest updates.

Sarita Patel
Sarita Patel author of SPACE Function is from United States.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!