Oracle's String splitting

Source: Internet
Author: User

At work, it is found that Oracle does not provide a string cutting function, find a better way to cut on the internet, record:

1. New data type

CREATE OR REPLACE TYPE str_split as TABLE of VARCHAR2 (500);

2. New Cut function

CREATE OR REPLACE FUNCTIONSplitstr (p_stringinch VARCHAR2, P_delimiterinch VARCHAR2)  RETURNstr_split pipelined isV_length Number:=LENGTH (p_string); V_start Number:= 1; V_index Number; BEGIN   while(V_start<=v_length) LOOP V_index:=INSTR (p_string, P_delimiter, V_start); IFV_index= 0  Then  PIPEROW (SUBSTR (p_string, V_start)); V_start:=V_length+ 1; ELSE  PIPEROW (SUBSTR (p_string, V_start, V_index-V_start)); V_start:=V_index+ 1; END IF; ENDLOOP; RETURN; ENDSPLITSTR;

3. Conduct the test

Select *  from Table (Splitstr ('hello,cnblogs! ',', '

4. Convert Rows to columns display

SELECTa.column_value V1, B.column_value V2 from(SELECT *           from(SELECTROWNUM RN, T.*                   from TABLE(Splitstr ('hello,cnblogs!',',')) (T)) A, (SELECT *           from(SELECTROWNUM RN, T.*                   from TABLE(Splitstr ('hello,cnblogs!',',')) ( T)) BWHEREA.rn= 1    andB.rn= 2;

Test results to be supplemented

Oracle's String splitting

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.