Description of the DB2 split operation procedure

Source: Internet
Author: User

The following articles mainly describe the actual operation process for implementing split in DB2. If you are interested in the actual operation process for implementing split in DB2, you can use the following articles to get a better understanding of the specific solution and hope it will be helpful for your future study.

Ask you a question and use the function to implement the split function to split strings.

If the input string is 'a, B, C ',

The output result is line.

Row 2 B

Row 3 c

Evaluate the specific DB2 implementation functions.

The implementation method of oracle is as follows:

 
 
  1. CREATE OR REPLACE TYPE ty_str_split IS TABLE OF VARCHAR2 (4000);  
  2. CREATE OR REPLACE FUNCTION fn_split (p_str IN VARCHAR2, p_delimiter IN VARCHAR2)  
  3. RETURN ty_str_split  
  4. IS  
  5. j INT := 0;  
  6. i INT := 1;  
  7. len INT := 0;  
  8. len1 INT := 0;  
  9. str VARCHAR2 (4000);  
  10. str_split ty_str_split := ty_str_split ();  
  11. BEGIN  
  12. len := LENGTH (p_str);  
  13. len1 := LENGTH (p_delimiter);  
  14. WHILE j < len 
  15. LOOP  
  16. j := INSTR (p_str, p_delimiter, i);  
  17. IF j = 0 
  18. THEN  
  19. j := len;  
  20. str := SUBSTR (p_str, i);  
  21. str_split.EXTEND;  
  22. str_split (str_split.COUNT) := str;  
  23. IF i >= len  
  24. THEN  
  25. EXIT;  
  26. END IF;  
  27. ELSE  
  28. str := SUBSTR (p_str, i, j - i);  
  29. i := j + len1;  
  30. str_split.EXTEND;  
  31. str_split (str_split.COUNT) := str;  
  32. END IF;  
  33. END LOOP;  
  34. RETURN str_split;  
  35. END fn_split;  
  36. /  

Test: SELECT * from table (fn_split ('a, B, C ',','));

Result:

A

B

C

Best Answer leo

You can write a udf for segmentation, but I prefer SQL:

 
 
  1. with n(str, ori, pos) as (  
  2. values ('abc,bc,cd,d,ff,', 1, posstr('abc,bc,cd,d,ff,', ','))  
  3. union all   
  4. select str, pos+1, locate(',', str, pos+1)  
  5. from n   
  6. where locate(',', str, pos+1)>0)  
  7. select str, ori, pos, substr(str, ori, pos-ori) as result from n   

You can write a udf for segmentation, but I prefer SQL:

 
 
  1. with n(str, ori, pos) as (  
  2. values ('abc,bc,cd,d,ff,', 1, posstr('abc,bc,cd,d,ff,', ','))  
  3. union all   
  4. select str, pos+1, locate(',', str, pos+1)  
  5. from n   
  6. where locate(',', str, pos+1)>0)  
  7. select str, ori, pos, substr(str, ori, pos-ori) as result from n   

The above content is an introduction to the implementation of split in DB2. I hope you will have some gains.

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.