Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Beginner
Nov 15, 2005

Constructing Strings of Repeated Characters in T-SQL

Sometimes you need to display a string of characters of a specified length for a column value in a SQL query. To this end, SQL Server supports a function called Replicate() which takes two parameters: the character you want to print and the count (the number of times you want to print the character). The first parameter is the character you want to print and the second is the count up to which you need to print the character.

For example, suppose you need to print an asterisk character ('*') some number of times based on a value contained in a Quantity column. Here's how you can do that:


SELECT Quantity, REPLICATE( '*', Quantity)
FROM [Order Details]
WHERE Discount IS NOT NULL

12	************
10	**********
Srinath MS
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap