SQL> WITH T AS
2 (SELECT 'ABCD,123,DEFOIFCD,87765' AS STR FROM DUAL)
3 SELECT LEVEL AS N, REGEXP_SUBSTR(STR, '[^,]+', 1, LEVEL) AS VAL
4 FROM T
5 CONNECT BY REGEXP_SUBSTR(STR, '[^,]+', 1, LEVEL) IS NOT NULL
6 /
N VAL
---------- -----------------------
1 ABCD
2 123
3 DEFOIFCD
4 87765
2 (SELECT 'ABCD,123,DEFOIFCD,87765' AS STR FROM DUAL)
3 SELECT LEVEL AS N, REGEXP_SUBSTR(STR, '[^,]+', 1, LEVEL) AS VAL
4 FROM T
5 CONNECT BY REGEXP_SUBSTR(STR, '[^,]+', 1, LEVEL) IS NOT NULL
6 /
N VAL
---------- -----------------------
1 ABCD
2 123
3 DEFOIFCD
4 87765
Apart from Regular Expressions, a few other alternatives are using:
- XMLTable
- MODEL clause
Setup
SQL> CREATE TABLE t (
2 ID NUMBER GENERATED ALWAYS AS IDENTITY,
3 text VARCHAR2(100)
4 );
Table created.
SQL>
SQL> INSERT INTO t (text) VALUES ('word1, word2, word3');
1 row created.
SQL> INSERT INTO t (text) VALUES ('word4, word5, word6');
1 row created.
SQL> INSERT INTO t (text) VALUES ('word7, word8, word9');
1 row created.
SQL> COMMIT;
Commit complete.
SQL>
SQL> SELECT * FROM t;
ID TEXT
---------- ----------------------------------------------
1 word1, word2, word3
2 word4, word5, word6
3 word7, word8, word9
SQL>
There is a huge difference between the below two:
If you do not restrict the rows, then the CONNECT BY clause would produce multiple rows and will not give the desired output.
Apart from Regular Expressions, a few other alternatives are using:
Setup
Using XMLTABLE:
Using MODEL clause:
|
No comments:
Post a Comment