简体   繁体   English

如何从逗号分隔的字符串中提取前两个值

[英]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.

相关问题 如何比较两个不同的逗号分隔的字符串值,以及如何检查第一个中是否存在不同逗号分隔的字符串中的任何值 - How to compare two different comma separated string values and check if ANY of the values from a different comma separated string exists in the first 从逗号分隔的 GUID 中提取逗号分隔的值 - Extract comma separated values from comma separated GUIDs 从oracle中的逗号分隔字符串中提取单词 - Extract words from a comma separated string in oracle 将逗号分隔的两个字符串值转换为表 - Convert comma separated two string values to table 如何从SQL查询中用逗号分隔的字符串中选择值? - How to select values from string separated by comma in SQL Query? 如何从 sql 中的逗号分隔字符串中删除 _ 符号后的值? - How to remove values after _ sign from comma separated string in sql? 如何从MySQL中逗号分隔的字符串值计算中位数? - How to calculate median from comma separated string values in MySQL? 如何获取不在逗号分隔的字符串的第一个或最后一个索引中但在MySQL的字符串中间索引的值 - How to get values not indexed in the first or in the last in a comma separated string but in the middle of the string in MySQL 如何从 TRINO 中的键/值 object 数组中提取值并创建以逗号分隔的值数组 - How to extract values from key/value object array in TRINO and create comma separated array of the values 从SQL Server中以逗号分隔的字符串中提取字符串的一部分 - Extract a part of the string from comma separated string in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM