Quantcast
Channel: Parse data from a table row and output the values formatted
Viewing all articles
Browse latest Browse all 5

Parse data from a table row and output the values formatted

$
0
0
Declare @rept  table(Id  int Identity(1,1), ReptDesc varchar(max))
Insert into @rept (ReptDesc)
Values(
'Category: Projects; Accounting and Operations; Sales 
Description: this is a transaction report'),
('Category: Technical, Accounting
Description:'),
('Category: Accounting'),
('Category: Accounting, Sales'),
(Null),
(Null)


--SQL Server 2016 or up versions
;with mycte as (
Select * , replace(replace(replace(replace(replace(replace(ReptDesc,char(10),''),char(13),''),
'Category:','{"Category":["'),',','","'), ';','","'),'Description:','"], "Description":["')+'"]}' jsonCol
from @rept
 )

 select id 
 ,c.value  as Category  
 ,d.value  as Description  
 
 
 
 from mycte
 CROSS APPLY OPENJSON(jsoncol, '$.Category') AS c
 Outer APPLY OPENJSON(jsoncol, '$.Description') AS d
  
/*
id	Category	Description
1	 Projects	 this is a transaction report
1	 Accounting and Operations	 this is a transaction report
1	 Sales 	 this is a transaction report
2	 Technical	
2	 Accounting	
3	 Accounting	NULL
4	 Accounting	NULL
4	 Sales	NULL
*/

 


Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>