[英]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. 我需要从逗号分隔的字符串中提取前两个值,并使用
regexp_substr
函数将其存储在数组变量中。
The string looks like 'aaa,bbb,ccc,ddd,eee'
. 字符串看起来像
'aaa,bbb,ccc,ddd,eee'
。
I need 'aaa' and 'bbb' to store into an array(nested table). 我需要'aaa'和'bbb'存储到数组(嵌套表)中。
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
http://sqlfiddle.com/#!4/2840fd/31
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.