1 year ago

#378834

test-img

B Nageswara Rao

In SQL Server I have Json format rows in column level need to create multiple columns # fields are not order we can create based on that

{zipCode=47715, state=IN, city=Evansville, countryCode=US, line1=7424 Eagle Crest Blvd}
{zipCode=10965, state=NY, city=Pearl River, countryCode=US, line1=One Blue Hill Plaza}
{zipCode=30339, state=GA, city=Atlanta, countryCode=US, line1=1100 Circle 75 Parkway SE, Ste. 140}
{zipCode=35210, state=AL, city=Birmingham, countryCode=US, line1=700 Maple Street, Ste. B}
{zipCode=30339, state=GA, city=Atlanta, countryCode=US, line1=2839 Paces Ferry Road, Ste. 1200}
{zipCode=35242, state=AL, city=Birmingham, countryCode=US, line1=700 Corporate Parkway}
{zipCode=64112, state=MO, city=Kansas City, countryCode=US, line1=444 W 47th Street, Ste. 900}

line1
line2
line3
City
state
ZipCode
CountryCode

,CONVERT(VARCHAR(200),SUBSTRING(REPLACE(REPLACE(Business_mailing_address,'{',''),'}',''), CASE WHEN REPLACE(REPLACE(Business_mailing_address,'{',''),'}','') LIKE '%line1:%'                    THEN CHARINDEX('line1:'                                             ,REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6        ELSE NULL END, CASE WHEN REPLACE(REPLACE(Business_mailing_address,'{',''),'}','') LIKE '%line2:%' THEN (CHARINDEX('line2:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) - 1) - (CHARINDEX('line1:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6) WHEN REPLACE(REPLACE(Business_mailing_address,'{',''),'}','') LIKE '%line3:%' THEN (CHARINDEX('line3:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) - 1) - (CHARINDEX('line1:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6) ELSE (CHARINDEX('state:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) - 1) - (CHARINDEX('line1:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6) END)) AS mailing_address_line1
,CONVERT(VARCHAR(200),SUBSTRING(REPLACE(REPLACE(Business_mailing_address,'{',''),'}',''), CASE WHEN REPLACE(REPLACE(Business_mailing_address,'{',''),'}','') LIKE '%line2:%'                     THEN CHARINDEX('line2:'                                                ,REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6        ELSE NULL END, CASE WHEN REPLACE(REPLACE(Business_mailing_address,'{',''),'}','') LIKE '%line3:%' THEN (CHARINDEX('line3:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) - 1) - (CHARINDEX('line2:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6) ELSE (CHARINDEX('state:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) - 1) - (CHARINDEX('line2:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6) END)) AS mailing_address_line2
,CONVERT(VARCHAR(200),SUBSTRING(REPLACE(REPLACE(Business_mailing_address,'{',''),'}',''), CASE WHEN REPLACE(REPLACE(Business_mailing_address,'{',''),'}','') LIKE '%line3:%'                     THEN CHARINDEX('line3:'                                                ,REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6        ELSE NULL END, (CHARINDEX('state:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) - 1) - (CHARINDEX('line3:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6))) AS mailing_address_line3
,CONVERT(VARCHAR(100),SUBSTRING(REPLACE(REPLACE(Business_mailing_address,'{',''),'}',''), CASE WHEN REPLACE(REPLACE(Business_mailing_address,'{',''),'}','') LIKE '%city%'                                         THEN CHARINDEX(':'                                                                ,REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 1        ELSE NULL END, CHARINDEX(',',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) - (CHARINDEX(':',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 1)))AS mailing_address_city
,CONVERT(VARCHAR(10) ,SUBSTRING(REPLACE(REPLACE(Business_mailing_address,'{',''),'}',''), CASE WHEN REPLACE(REPLACE(Business_mailing_address,'{',''),'}','') LIKE '%state:%'                     THEN CHARINDEX('state:'                                                ,REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6        ELSE NULL END, (CHARINDEX('zipCode:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) - 1) - (CHARINDEX('state:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 6))) AS mailing_address_state
,CONVERT(VARCHAR(50) ,SUBSTRING(REPLACE(REPLACE(Business_mailing_address,'{',''),'}',''), CASE WHEN REPLACE(REPLACE(Business_mailing_address,'{',''),'}','') LIKE '%zipCode:%'                               THEN CHARINDEX('zipCode:'                   ,REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 8        ELSE NULL END, (CHARINDEX('countryCode:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) - 1) - (CHARINDEX('zipCode:',REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 8))) AS mailing_address_zipcode
,CONVERT(VARCHAR(10) ,SUBSTRING(REPLACE(REPLACE(Business_mailing_address,'{',''),'}',''), CASE WHEN REPLACE(REPLACE(Business_mailing_address,'{',''),'}','') LIKE '%countryCode:%'     THEN CHARINDEX('countryCode:'         ,REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')) + 12      ELSE NULL END, LEN(REPLACE(REPLACE(Business_mailing_address,'{',''),'}','')))) AS mailing_address_countrycode

but how to after comma (,) space( ) how to give and handle Please help me out

sql-server

tsql

sql-server-2008

sql-server-2012

0 Answers

Your Answer

Accepted video resources