devxlogo

Constructing Strings of Repeated Characters in T-SQL

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 NULL12	************10	**********
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist