简体   繁体   中英

How extract first two values from a comma separated string

I need to extract the first two values from the comma separated string and store in an array variable using regexp_substr function.

The string looks like 'aaa,bbb,ccc,ddd,eee' .

I need 'aaa' and 'bbb' to store into an array(nested table).

Please help as to how to achieve this.

Something like this, maybe?

DECLARE 

TYPE dtype  IS TABLE OF VARCHAR2(10);
x dtype := dtype();
s VARCHAR2(100) := 'aaa,bbb,ccc,ddd,eee';
BEGIN
   x.extend(2);
   x(1) := REGEXP_SUBSTR(s,'[^,]+', 1,1) ;
   x(2) := REGEXP_SUBSTR(s,'[^,]+', 1,2) ;

  DBMS_OUTPUT.PUT_LINE(x(1));
  DBMS_OUTPUT.PUT_LINE(x(2));

END;
/

aaa
bbb
CREATE TABLE Table1
    (name varchar(23))
;

INSERT INTO Table1
    (name)
VALUES
    ('aaa,bbb,ccc,ddd,eee')
;
SELECT SUBSTR(name, 1, Instr(name, ',', 1, 1) -1) AS part1,
       SUBSTR(name, Instr(name, ',') + 1, 
              Instr(name, ',', 1, 2) - Instr(name, ',') - 1) AS part_2
FROM Table1

Output

PART1   PART_2
aaa     bbb

Live Demo

http://sqlfiddle.com/#!4/2840fd/31

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM