I have a table TABLE1
as below :
YEAR | GL_CODE
-------------------------------------------
2019 | 141.0000.1001.732155.0000.000.0000.
2019 | 141.0000.0000.143402.0000.131.0000.
2019 | 541.000.00.00.00.149104.0000.000.00
2019 | 541.101.04.00.00.731104.0000.000.00
2019 | 141.0000.0000.151310.0000.000.0000.
2019 | 541.102.06.00.16.714101.7098.000.00
2019 | 111.00000.0511.766701.0000.000.0000
2019 | 111.00000.0512.520111.5003.331.0000
I need it to separated to different Segments or Columns as shown below:
YEAR |SEGMENT1|SEGMENT2|SEGMENT3|SEGMENT4|SEGMENT5|SEGMENT6|SEGMENT7 |SEGMENT8|SEGMENT9
------------------------------------------------------------------------------------------
2019 | 141 | 0000 | 1001 |732155 | 0000 | 000 | 0000 | NULL | NULL
2019 | 141 | 0000 | 0000 |143402 | 0000 | 131 | 0000 | NULL | NULL
2019 | 541 | 000 | 00 | 00 | 00 |149104 | 0000 | 000 | 00
2019 | 541 | 101 | 04 | 00 | 00 |731104 | 0000 | 000 | 00
2019 | 141 | 0000 | 0000 |151310 | 0000 | 000 | 0000 | NULL | NULL
2019 | 541 | 102 | 06 | 00 |714101 |714101 | 7098 | 000 | 00
2019 | 111 | 00000 | 0511 |766701 | 0000 | 000 | 0000 | NULL | NULL
2019 | 111 | 00000 | 0512 |520111 | 5003 | 331 | 0000 | NULL | NULL
Note that all the entries in doesnt have a fixed number of segment.
I tried by using :
select YEAR,PERIOD,SUBSTRING(GL_CODE,1,3) as segment1,
SUBSTRING(GL_CODE,
charindex('.', GL_CODE, 1)+1,
((charindex('.',GL_CODE, (charindex('.', GL_CODE, 1)+1))+1)-(charindex('.', GL_CODE, 1)+1)-1)
) as segment2,
SUBSTRING(GL_CODE,
charindex('.',GL_CODE, (charindex('.', GL_CODE, 1)+1))+1,
(charindex('.',GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1)-
(charindex('.',GL_CODE, ( charindex('.', GL_CODE, 1)+1))+1)-1
) as segment3,
SUBSTRING(GL_CODE,
charindex('.',GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1,
(charindex('.',GL_CODE,(charindex('.', GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1))+1 )-(charindex('.',GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1)-1
) as segment4,
SUBSTRING(GL_CODE,
charindex('.',GL_CODE,(charindex('.', GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1))+1,(charindex('.',GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE, (charindex('.', GL_CODE, (charindex('.',GL_CODE, 1)+1))+1))+1))+1))+1)-(charindex('.',GL_CODE,(charindex('.', GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1))+1)-1
) as segment5,
SUBSTRING(GL_CODE,
charindex('.',GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE, (charindex('.', GL_CODE,
(charindex('.',GL_CODE, 1)+1))+1))+1))+1))+1,
(charindex('.',GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,
(charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1))+1))+1))+1)
-(charindex('.',GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE, (charindex('.', GL_CODE, (charindex('.',GL_CODE, 1)+1))+1))+1))+1))+1)-1
) as segment6,
SUBSTRING(GL_CODE,
charindex('.',GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,
(charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1))+1))+1))+1,
(charindex('.',GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,
(charindex('.', GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1))+1))+1))+1))+1)
-(charindex('.',GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1))+1))+1))+1)-1
) as segment7,
SUBSTRING(GL_CODE,
charindex('.',GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,
(charindex('.', GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1))+1))+1))+1))+1,
(charindex('.',GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,
(charindex('.', GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE,
(charindex('.', GL_CODE, 1)+1))+1))+1))+1))+1))+1))+1))+1)
-(charindex('.',GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,(charindex('.', GL_CODE,
(charindex('.', GL_CODE, (charindex('.', GL_CODE, (charindex('.', GL_CODE, 1)+1))+1))+1))+1))+1))+1))+1)-1
) as segment8
from TABLE1;
Im Getting values upto 6th Segment... After the 6th Segment I'm getting errors. Can anyone suggest a change in code or an alternative method?
Thanks in Advance
Akshay