My table named color_code
looks like this:
color_id color_name
--------- ----------
101 (red,blue,green)
102 (cyan,magenta)
103 (white,deep,light,yellow)
104 (null)
But My data should be in below format and this resultant data should inserted into color_insert
table
This should be my output:
color_id color_name
--------- ------------
101 red
101 blue
101 green
102 cyan
102 magenta
103 white
103 deep
103 light
103 yellow
104
I have tried this, but not satisfied with my logic. Could you please help me on this
DECLARE
BEGIN
FOR i IN 1..4
LOOP
INSERT INTO color_insert
SELECT color_id,
TRIM(REGEXP_SUBSTR(color_name, '[^,]+', 1, i)) color_name
FROM color_test
CONNECT BY REGEXP_SUBSTR(color_id, '[^,]+', 1, level) IS NOT NULL
ORDER BY color_id;
END LOOP;
END;