How to iterate a string and add string to every position?

14,257
declare input as varchar(1000) -- Choose the appropriate size
declare output as varchar(1000) -- Choose the appropriate size

select @input = '123', @output = ''

declare @i int

select @i = 0

while @i < len(@input)
begin
    select @i = @i + 1

    select @output = @output + 'ABC' + substring(@input, @i, 1)
end
Share:
14,257
noname
Author by

noname

Updated on June 15, 2022

Comments

  • noname
    noname about 2 years

    I need something like this in TSQL

    string myString = "123";
    for (int i = 0;  i < myString.Length; i++)
    {
       myString.Insert("ABC", i);
    }
    Output "ABC1ABC2ABC3"