0

I have a varchar collumn which may contain format like this:

123,124,125,126

Now i want to get all number and put it in a single column like this in select command

123
124
125
126

Any idea?

1

3 Answers 3

1

Try this too,

with test as 
(
SELECT '123,124,125,126' str FROM dual  
)  
SELECT regexp_substr (str, '[^,]+', 1, ROWNUM) SPLIT  
FROM   TEST  
CONNECT BY LEVEL <= LENGTH (regexp_replace (str, '[^,]+'))  + 1;

Try this if you have an additional comma at the end,

with test as 
(
SELECT '123,124,125,126,' str FROM dual  
)
SELECT regexp_substr(str,'[^,]+', 1, LEVEL) FROM test
connect by regexp_substr(str, '[^,]+', 1, level) is not null;
Sign up to request clarification or add additional context in comments.

2 Comments

Assume the problem '123,124,125,126,' == a comma in the end. You return a empty a empty row.
@realspirituals, Thanks for pointing out, I have added another answer to overcome this like issue.
0

Answering umpteenth time...

WITH CTE
    AS (SELECT
             '123,124,125,126' AS COL1
        FROM
             DUAL)
SELECT
      REGEXP_SUBSTR ( COL1,
                   '[^,]+',
                   1,
                   RN )
          COL1
FROM
          CTE
      CROSS JOIN
          (SELECT
                ROWNUM RN
           FROM
                (SELECT
                       MAX ( LENGTH ( REGEXP_REPLACE ( COL1,
                                                '[^,]+' ) ) )
                       + 1
                           MAX_L
                 FROM
                       CTE)
           CONNECT BY
                LEVEL <= MAX_L)
WHERE
      REGEXP_SUBSTR ( COL1,
                   '[^,]+',
                   1,
                   RN )
          IS NOT NULL
ORDER BY
      COL1;

Comments

0

Alternatively; substr, instr, lag and regexp_count together as :

select substr(str,second,first-second) as "Result String"       
  from
  (
    with t(str) as
    (   
     select '123,124,125,126' from dual
    )
     select replace(instr(str,',',1,level),0,length(str)+1) first,
            nvl(lag(instr(str,',',1,level)) over (order by level),0)+1 second,
            str              
       from dual
       cross join ( select str from t )
     connect by level <= regexp_count(str,',')+1
  );

Rextester Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.