Oracle split to comma, row and column conversions

Source: Internet
Author: User

 

Reproduced

1. For ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ' (comma outside the string)

    1. sql> SELECT column_value from TABLE (SYS.  Odcivarchar2list (' 1 ',' 2 ',' 3 ',' 4 ',' 5 '));
    2. Column_value
    3. --------------------------------------------------------------------------------
    4. 1
    5. 2
    6. 3
    7. 4
    8. 5


2. For ' 1,2,3,4,5 ' (comma inside the string)

 
    1. sql> select regexp_substr ( Span class= "string" > ' 1,2,3,4,5 ', "[^,]+ ', 1,rownum)  from  dual  
    2.   2  connect  by rownum<=length ( ' 1,2,3,4,5 ')-length ( replace ( ' 1,2,3,4,5 ',
    3. &NBSP;&NBSP;3&NBSP;&NBSP;;&NBSP;&NBSP;
    4.    
    5. regexp_substr (
    6. span class= "comment" >------------------------------  
    7. 1&NBSP;&NBSP;
    8. 2  
    9. 3&NBSP;&NBSP;
    10. 4  
    11. 5&NBSP;&NBSP;


3. Using functions

    1. CREATE OR REPLACE TYPE ty_str_split is TABLE of VARCHAR2 (4000);

  1. CREATE OR REPLACE FUNCTION fn_split (p_str in CLOB, p_delimiter in VARCHAR2)
  2. RETURN Ty_str_split
  3. Is
  4. J INT: = 0;
  5. I INT: = 1;
  6. Len INT: = 0;
  7. Len1 INT: = 0;
  8. STR VARCHAR2 (4000);
  9. Str_split Ty_str_split: = Ty_str_split ();
  10. BEGIN
  11. Len: = LENGTH (P_STR);
  12. Len1: = LENGTH (P_delimiter);
  13. While J < Len
  14. LOOP
  15. J: = INSTR (P_str, P_delimiter, i);
  16. IF j = 0
  17. Then
  18. J: = Len;
  19. str: = SUBSTR (P_str, i);
  20. Str_split. EXTEND;
  21. Str_split (str_split.   COUNT): = str;
  22. IF I >= len
  23. Then
  24. EXIT;
  25. END IF;
  26. ELSE
  27. str: = SUBSTR (P_str, I, j-i);
  28. I: = j + len1;
  29. Str_split. EXTEND;
  30. Str_split (str_split.   COUNT): = str;
  31. END IF;
  32. END LOOP;
  33. RETURN Str_split;
  34. END Fn_split;


Test:

  1. <p>SQL> SELECT * from table (Fn_split (' 1,2,3,4,5 ',', ')); --The second single quotation mark is the character that needs to be delimited in the preceding string </p><p>column_value
  2. --------------------------------------------------------------------------------
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5</p><p>sql> SELECT * from table (Fn_split (' 1,2,3,4. 5 ','. ')); </p><p>column_value
  8. --------------------------------------------------------------------------------
  9. 1,2,3,4
  10. 5</p><p>sql></p>


Reference:

Http://www.itpub.net/thread-1346178-1-1.html

Many of them have summed up a number of ways to change ranks. Today found a new method (), and share it with everyone.
1.SYS. Odcivarchar2list:
SELECT Column_value from TABLE (SYS. Odcivarchar2list (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '));
Column_value
--------------------------------------------------------------------------------
1
2
3
4
5
Oracle 10G and above only support sys.odcivarchar2list, in fact, sys.odcivarchar2list is just a type,
So in the 9I version, you can use this feature by creating a type:
CREATE OR REPLACE TYPE my_odcivarchar2list as Varray (32767) of VARCHAR2 (4000);

SELECT Column_value from TABLE (my_odcivarchar2list (' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 '));
Column_value
--------------------------------------------------------------------------------
1
2
3
4
5
However, when ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ' as a string (' 1,2,3,4,5 ') there is no way to convert:
SELECT Column_value from TABLE (my_odcivarchar2list (' 1,2,3,4,5 '));
Column_value
--------------------------------------------------------------------------------
1,2,3,4,5

Summary: (1) The table function queries the contents of the array through SQL statements;
(2) Odcivarchar2list can be used in versions 9I and above. The sys.odcivarchar2list can be directly used in 9I by creating type,10g and above;
(3) Odcivarchar2list applies to character sets, does not apply to a single string, and if it is a single string, it can be implemented by referring to the 2 (below) method.

Welcome to discuss, put forward more and better ways ~ ~

Reference----------------------------------------------------------------
2. Other ways to make a career change (Daniel has long been summed up, for reference only)
(1) using Connect by (using 9i,10g,11g)
With T as (SELECT ' 1,2,3,4,5 ' as STR from DUAL)
SELECT STR1
From (SELECT DISTINCT
SUBSTR (T.ca,instr (t.ca, ', ', 1, c.lv) + 1,
INSTR (t.ca, ', ', 1, c.lv + 1)-(INSTR (t.ca, ', ', 1, c.lv) + 1)) as STR1
From (SELECT ', ' | | STR | | ', ' as Ca,length (STR | | ', ')-nvl (LENGTH (REPLACE (STR, ', ')), 0) as CNT from T) T,
(SELECT level LV from DUAL CONNECT to level <= 9) C
WHERE c.lv <= t.cnt
ORDER by STR1);
(2). Regular expressions (using 10G and later)
With TEST as (SELECT ' 1,2,3,4,5 ' as STR from DUAL)
SELECT DISTINCT regexp_substr (STR, ' [^,]+ ', 1, level)
From TEST
CONNECT by ROWNUM <= 5;

Oracle split to comma, row and column conversions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.