Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need a function in sql server to insert a space in each postion of an input string

I need a function in sql server to insert a space in each position of an input string. For example, if the input of the function is

"example"

the output of the function should be

word1   word2
e       xample
ex      ample
exa     mple
exam    ple
examp   le
exampl  e
like image 901
jozi Avatar asked Dec 01 '25 10:12

jozi


1 Answers

declare @S varchar(20) = 'example'

select left(@S, number) as word1,
       stuff(@S, 1, number, '') as word2
from master..spt_values
where type = 'P' and
      number between 1 and len(@S)-1

Or like this if you want one column with the space inserted:

select stuff(@S, number, 0, ' ') as word
from master..spt_values
where type = 'P' and
      number between 2 and len(@S)      
like image 62
Mikael Eriksson Avatar answered Dec 02 '25 22:12

Mikael Eriksson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!