Declare @rept table(Id int Identity(1,1), ReptDesc varchar(max)) Insert into @rept (ReptDesc) Values( 'SomeCategory: Projects; Accounting and Operations; Sales Description: this is a transaction report'), ('Category1: Technical, Accounting Description:'), ('Category2: Accounting'), ('Category3: Accounting, Sales'), (Null), (Null) ;with mycte as ( Select id, Cast(Stuff( replace(replace(replace(replace( reverse(stuff(reverse(replace(replace(ReptDesc,char(10),''),char(13),'')),1,charindex(reverse('Description'),reverse(ReptDesc)),'') ), ':','<H><r>'),';','</r><r>'),',','</r><r>'),'Descriptio','')+'</r></H>',1,charindex('<H>', replace(replace(replace(replace( reverse(stuff(reverse(replace(replace(ReptDesc,char(10),''),char(13),'')),1,charindex(reverse('Description'),reverse(ReptDesc)),'') ), ':','<H><r>'),';','</r><r>'),',','</r><r>'),'Descriptio','')+'</r></H>') -1,'') as XML) XMLcol ,Replace(reverse(substring(reverse(ReptDesc),1,charindex(reverse('Description'),reverse(ReptDesc)) )),'n:','') Description from @rept ) Select DISTINCT Id, S.a.value('.','VARCHAR(100)') CatCol,Description FROM mycte d Cross APPLY d.XMLcol.nodes('/H/r') S(a)
↧
Parse data from a table row and output the values formatted
↧