Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Lookup every character in string and replace with character from another string

$
0
0

I have a table with two string type columns named IL and CL. I have to compare both the strings character by character and wherever there is a question mark in column CL string, I have to replace it with the character found in the other column (IL), in the exact same position.

For example, the CL string after comparing with IL and replacing the question marks would be the New CL as shown below

CLILNEW CL
???-123201-000000-000-???104-234561-644221-123-947104-123201-000000-000-947

I have the below code working, where there is a while loop inside of a cursor going through each character of the string, concatenating into a new string, and then doing an update at the end with the new value. However, performance with this piece of code is horribly slow as the table has 100K+ records and it's looping through each row 25 time (string length). I am looking to see if perhaps there is a way to rewrite this logic set based for performance improvement.

declare CharC insensitive cursor for   select ID, IL, CL  from AcctStrings  where CL like '%?%'open CharCfetch next from CharC into @ID, @IL, @CLwhile @@fetch_status = 0 begin    set @NewCL = ''    set @i = 1     while @i <= 25 begin      set @TestChar = substring(@CL,@i,1)      set @OtherChar = substring(@IL,@i,1)      if (@TestChar = '?') begin        set @NewCL = @NewCL + @OtherChar       end       else         set @NewCL = @NewCL + @TestChar      set @i = @i + 1    end    update AcctStrings      set CL = @NewCL    where ID = @ID  end  fetch next from CharC into @ID, @IL, @CLenddeallocate CharC

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>