-1

I do not have the access needed to create a function; but I need to be able to parse the following text.

ID           value
684286211   96756263;97051390
683855568   96825924;96862563;96862652;96862684;96862692 

needing:

ID               value
684286211       96756263 
684286211       97051390
683855568       96825924
683855568       96862563
683855568       96862652
683855568       96862684
683855568       96862692

I have tried using the Parsename statement but it only works on 4 or less sections; I need to allow up to nine values.

using sql 2012

any help would be appreciated!

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Steve
  • 11
  • 3

1 Answers1

1

You can use a recursive CTE for this purpose:

with CTE as (
      select id, value,
             left(value, charindex(';', value)) as val,
             substring(value, charindex(';', value) + 1, len(value))+';' as restval
     from t
     where value like '%;%'
     union all
     select id, value, left(restval, charindex(';', restval)) as val,
            substring(restrval, charindex(';', restval) + 1, len(restval))
     from cte
     where value like '%;%'
    )
select id, val
from cte
union all
select id, value
from t
where value not like '%;%';

It is a pain to avoid errors for values that have no semicolons. I think this is one method for doing this.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786