February 18, 2008

Repeat group of characters 'n' number of times

You can use the rich in-built function set of SQL Server 2005 to perform a variety of tasks. Let us explore how to repeat a group of characters 'n' number of times and also insert space in between two strings.

Repeat a group of characters 'n' number of times

Use the REPLICATE() function to repeat a character expression for a specified number of times

SELECT REPLICATE('-',15) as Underline
displays: the character '-' 15 times ---------------

Insert space in between two strings

SPACE() function returns a string of repeated spaces depending on the number specified

SELECT 'Far' + SPACE(10) + 'Away'
displays: 'Far         Away'


About The Author

Suprotim Agarwal, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

2 comments:

danh said...

I don't think that your SPACE() display string looks right...

Suprotim Agarwal said...

Thanks Fixed!