sql server - SQL replace occurrances based on a table -
hi i've sql issue solve; i've these tables:
table varchar column tst
tst
'2','5','8'
'2','6'
'4','12'
table b int column rep
rep
2
6
i'm looking query (without cycle while) update table in following way:
tst
'r','5','8'
'r','r'
'4','12'
using char 'r' replace occurrances of table b in table a
thanks in advance
update t1 set tst = stuff(z,1,1,'') --remove leading comma final result ( select --convert original string xml tst ,cast('<a>'+replace(tst ,',','</a><a>')+'</a>' xml) x tst ) t1 cross apply ( select --replace value 'r' when matched in rep ','+case when rep null y.value('.','varchar(max)') else '''r''' end x.nodes('a') t2(y) --explode xml separate values left join rep t3 --match value rep on y.value('.','varchar(max)') = quotename(rep,char(39)) xml path('') --recompact xml comma-delimited string ) t4(z)
Comments
Post a Comment