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?
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?
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;
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;
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
);