Home php教程 PHP开发 Detailed explanation of Oracle custom split function examples

Detailed explanation of Oracle custom split function examples

Jan 06, 2017 pm 01:03 PM

Oracle custom split function

Oracle does not provide a split function, but you can create a function yourself to achieve this function. For example, "abc defg hijkl nmopqr stuvw xyz", the separator is a space, but the number of spaces is variable.

Source code:

CREATE OR REPLACE TYPE ty_str_split IS TABLE OF VARCHAR2 (4000);
 
CREATE OR REPLACE FUNCTION fn_var_split (
  p_str IN VARCHAR2,
  p_delimiter IN VARCHAR2
)
  RETURN ty_str_split
IS
  j INT := 0;
  len INT := 0;
  str VARCHAR2 (4000);
  str_split ty_str_split := ty_str_split ();
  v_str VARCHAR2 (4000) := RTRIM (LTRIM (p_str, p_delimiter), p_delimiter);
BEGIN
  len := LENGTH (v_str);
 
  WHILE len > 0
  LOOP
    j := INSTR (v_str, p_delimiter, 1);
 
    IF j = 0
    THEN
      str := SUBSTR (v_str, 1);
      len := 0;
      str_split.EXTEND;
      str_split (str_split.COUNT) := str;
    ELSE
      str := SUBSTR (v_str, 1, j - 1);
      v_str := LTRIM (LTRIM (v_str, str), p_delimiter);
      len := LENGTH (v_str);
      str_split.EXTEND;
      str_split (str_split.COUNT) := str;
    END IF;
  END LOOP;
 
  RETURN str_split;
END fn_var_split;
/
Copy after login

Test:
Result:

1
12
123
1234
12345
Copy after login
DECLARE
  CURSOR c
  IS
    SELECT * FROM TABLE (CAST (fn_var_split (';1;12;;123;;;1234;;;;12345;', ';') AS ty_str_split));
  r c%ROWTYPE;
BEGIN
  OPEN c;
  LOOP
    FETCH c INTO r;
    EXIT WHEN c%NOTFOUND;
    DBMS_OUTPUT.put_line (r.column_value);
  END LOOP;
  CLOSE c;
END;
/
Copy after login

Thank you for reading, I hope it can help everyone, thank you for your support of this site!

For more Oracle custom split function examples and related articles, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)