Brief illustration :
If I want to replace a single string using multiple criteria from another table, I can use this statement :
-- assume table_2 has column 'value' containing records such as 'd', 'f','h'.
declare @x varchar(10)
set @x = 'abcdefghij'
Select @x = replace(@x,value,'') from table_2
Select @x
-- the result will be : abcegij.
Question :
How do I implement this if I have many initial strings i.e the 'abcdefghij' one from above example, stored in a table (table_1) and would like to be replaced for any occurrence of strings contained in another table (Table_2)
Edit :
DBMS being used in this question is sql server 2008 r2.
My current solution is somewhat similar to what JoseTeixeira has proposed i.e using cursor, manually iterating each record in table_1, process it using above snippet and then store each result into a temporary table. In other words, this temporary table will hold all records from table_1 which have been cleansed (replaced for any string occurrence) using "dictionary" in table_2.
And perhaps this is the most important thing I have missed when asking this question.. I'm looking for a solution without using cursor iteration.
Best Answer
Creating the schema - Tables that will be used for the example:
Procedural code starts here. First a temporary table is used to get the replace strings numbered so we can iterate on them:
The following CTE will pick up the original strings that we want to update (@table_1) and will iterate through each row of the table with the replacing strings, which are now numbered (@table_2_numbered), and recursively apply them to replace every desired occurence. Notice we keep track of the original strings so we know which replaced strings correspond to which original strings:
And here are the results: