Replace multiple Value from another table

1k views Asked by At

I have 2 tables.

Table1

ID |  String 
1  |  "A TEST B VALUE" 
2  |  "C TEST D DENT B"

Table 2

ID |  Name |  Value
1  |    A  |     1
1  |    B  |     2
2  |    B  |     3
2  |    C  |     4
2  |    D  |     5

The result I am hoping to get is

ID |  String
1  |  "1 TEST 2 VALUE"
2  |  "4 TEST 5 5ENT 3"

I am trying to do this using with clause recursive

with QueryTable as (
    select id, cast(String as nvarchar(max)) as 'String' ,1 as 'RN'
    from [TABLE1]
    group by id, String 
    union all
    select a.id, cast(replace(a.String, b.Name, b.value) as nvarchar(max)) as 'Query', RN+1 
    from QueryTable a
    inner join [TABLE2] b on a.id = b.id
)

Thank you so very much.

1

There are 1 answers

1
ElevenCent On

The solution Found in below link, so with cte recursive but compare between source and destination row num seem to solve the problem. https://dba.stackexchange.com/questions/237182/how-to-replace-multiple-parts-of-a-string-with-data-from-multiple-rows

if anyone interested I can post the final solution