5

I need something like this in TSQL

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

2 Answers 2

12
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
Sign up to request clarification or add additional context in comments.

Comments

0

You could use the length of the original string as your loop limit and the substring() function to append the nth char and 'ABC' to a buffer string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.