SQL Server : need assitance parsing delimted data and returning a long concatenated string -
SQL Server : need assitance parsing delimted data and returning a long concatenated string -
ok, have table mfbrw
column serv_cde_list_osi
this list separated space; each code 2 characters in length. entire string can 0 – 100
codes long.
id | active_products__c | serv_cde_list_osi ____________________________________________ 1 | | aa ae ag 2 | | aa ag 3 | | aa ae lp
and have table authcodes
columns authcode , servicename
id | authcodes | servicename __________________________________ 1 | aa | beer 2 | ae | coffee 3 | ag | coke 4 | lp | water
i need authcode.servicename
each code in mfbrw.serv_cde_list_osi
, write name, 1 long string @ mfbrw.active_products__c
id | active_products__c | serv_cde_list_osi ____________________________________________ 1 | beer; coffee; coke | aa ae ag 2 | beer; coke; | aa ag 3 | beer; coffee; water;| aa ae lp
i thought might work, not homecoming anything.
update salesforce.[dbo].[mfbrw] set mfbrw.active_products__c = authcodes.servicename [mfbrw] left bring together [authcodes] on [mfbrw].serv_cde_list_osi = [authcodes].authcode (((charindex(([mfbrw].[serv_cde_list_osi]),([authcodes].[authcode])))>0));
please assist.
complaints against normalizing info aside (because it's out of our control), here's generalized solution:
create table list (id int primary key, codes nvarchar(300)); create table code (code nchar(2), name nvarchar(100)); insert list values (1, 'aa ab ac ad') ,(2, 'aa ac') ,(3, 'ab'); insert code values ('aa', 'apple') ,('ab', 'banana') ,('ac', 'cantaloupe') ,('ad', 'dough'); go matches ( select list.id, code.code, code.name list bring together code on list.codes '%' + code.code + '%' ) select list.id ,names = ( select ( select name + '; ' matches matches.id = list.id xml path(''), type ).value('.[1]', 'nvarchar(max)') ) list
here's sql fiddle.
edit: this sql fiddle slight modification of above illustration - executes update
against hypothetical list
table.
sql sql-server parsing join
Comments
Post a Comment