-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!

6
  • 2
    What language are you using? How is your sql table set up? You need to give us more info. Commented Jan 2, 2016 at 19:10
  • can the delimiter be space or semi-colon or any other character ? What is the database? Commented Jan 2, 2016 at 19:14
  • I need to be able to parse in sql;it is a semicolon, however I can replace it with whateveris needed. Commented Jan 2, 2016 at 19:24
  • Can u specify the lang Commented Jan 2, 2016 at 19:24
  • TSQL-2012; I need a select query to parse this with Commented Jan 2, 2016 at 19:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.