T-SQL Function: Add a space between all characters

“For any input string, return a string that has an additional space between each two characters, even when one of the input characters is itself a space”

DECLARE @string VARCHAR(100)
DECLARE @result VARCHAR(100)
 
SET @String = ‘hello world’;
 
WITH n AS (
SELECT TOP (LEN(@string)) ROW_NUMBER() OVER(ORDER BY [object_id]) x
FROM sys.objects)
 
SELECT @result = CAST(
(SELECT SUBSTRING(@string, x, 1) [text()], ‘ ‘
FROM n
FOR XML PATH(”), TYPE) AS varchar)
 
SELECT @result

 

I would like to have feedback from my blog readers.

Please post your feedback, question, or comments about this article.

Advertisements

2 thoughts on “T-SQL Function: Add a space between all characters

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s