如何在oracle中分割以逗号(偶数出现)分隔的名称?

时间:2019-03-20 10:44:24

标签: sql oracle

select EL_VALUES_FIELD1, regexp_substr(f_person_name_multiple(EL_VALUES_FIELD2,0), '[^,  ]+', 1, level)
from DATA_FORM_VALUES_1322308
connect BY  regexp_substr(f_person_name_multiple(EL_VALUES_FIELD2,0),'[^,  ]+', 1, level) is not null

输入字符串如下:

Kanodia, Gaurav,Punani, Rohit,Singhal, Bhavya

预期结果如下

Kanodia, Gaurav
Punani, Rohit
Singhal, Bhavya

3 个答案:

答案 0 :(得分:3)

如果输入字符串始终看起来像您向我们显示的字符串,则

  • 将第二个逗号替换为其他内容(例如#
  • #(而不是,)上分割输入字符串
  • 在这种情况下,
  • level将始终为<= 2

SQL> with test (col) as (select 'Kanodia, Gaurav,Punani, Rohit' from dual)
  2  select regexp_substr(regexp_replace(col, ',', '#', 1, 2), '[^#]+', 1, level) res
  3  from test
  4  connect by level <= 2;

RES
-----------------------------
Kanodia, Gaurav
Punani, Rohit

SQL>

如果输入字符串的格式不同,则以上(显然)将无法正常工作。


[编辑]

这是您可能执行此操作的一种方法。我不太擅长复杂的正则表达式,因此我编写了一个函数,该函数甚至可以用#替换逗号,然后再使用该函数。

SQL> create or replace function f_rep (par_string in varchar2)
  2    return varchar2
  3  is
  4    -- replace every second occurrence of a comma with a #
  5    l_cnt  number := regexp_count(par_string, ',');
  6    l_str  varchar2(100) := par_string;
  7  begin
  8    for i in 1 .. l_cnt loop
  9      if mod(i, 2) = 0 then
 10         l_str := regexp_replace(l_str, ',', '#', 1, (i/2)+1);
 11      end if;
 12    end loop;
 13    return l_str;
 14  end;
 15  /

Function created.

SQL>

此查询显示了如果有多行,如何避免出现错误结果。

SQL> with test (col) as
  2    (select 'Kanodia, Gaurav,Punani, Rohit,Singhal, Bhavya'            from dual union all
  3     select 'Little, Foot, MT0, DbFiddle'                              from dual union all
  4     select 'January, February, March, April, May, June, July, August' from dual)
  5  select
  6    col,
  7    trim(regexp_substr(f_rep(col), '[^#]+', 1, column_value)) repcol_split
  8  from test,
  9       table(cast(multiset(select level from dual
 10                           connect by level <= regexp_count(f_rep(col), '#') + 1
 11                          ) as sys.odcinumberlist));

COL                                                      REPCOL_SPLIT
-------------------------------------------------------- --------------------
Kanodia, Gaurav,Punani, Rohit,Singhal, Bhavya            Kanodia, Gaurav
Kanodia, Gaurav,Punani, Rohit,Singhal, Bhavya            Punani, Rohit
Kanodia, Gaurav,Punani, Rohit,Singhal, Bhavya            Singhal, Bhavya
Little, Foot, MT0, DbFiddle                              Little, Foot
Little, Foot, MT0, DbFiddle                              MT0, DbFiddle
January, February, March, April, May, June, July, August January, February
January, February, March, April, May, June, July, August March, April
January, February, March, April, May, June, July, August May, June
January, February, March, April, May, June, July, August July, August

9 rows selected.

SQL>

答案 1 :(得分:0)

-- for one row
with s as
(select 'Kanodia, Gaurav,Punani, Rohit' str from dual)
select regexp_substr(str, '[^,]+,[^,]+', 1, level) str
from s
connect by level <= ceil(regexp_count(str, ',') / 2);

STR
------------------------------
Kanodia, Gaurav
Punani, Rohit

Elapsed: 00:00:00.00

-- for multiple rows
with s as
(select 1 id, 'Kanodia, Gaurav,Punani, Rohit' str from dual union all
 select 2 id, '1, 2, 3, 4' str from dual union all
 select 3 id, '6, 7, 8, 9' str from dual
)
select id, level num, regexp_substr(str, '[^,]+,[^,]+', 1, level) str
from s
connect by id = prior id and level <= ceil(regexp_count(str, ',') / 2)
and prior dbms_random.value is not null;

        ID        NUM STR
---------- ---------- ------------------------------
         1          1 Kanodia, Gaurav
         1          2 Punani, Rohit
         2          1 1, 2
         2          2  3, 4
         3          1 6, 7
         3          2  8, 9

答案 2 :(得分:0)

这不使用正则表达式(仅使用简单的字符串函数),并且适用于多个输入行:

Oracle设置

'Bob,Steve,Jane,Sally'

查询

CREATE TABLE test_data ( id, col ) AS
SELECT 1, 'Kanodia, Gaurav,Punani, Rohit,Singhal, Bhavya' FROM DUAL UNION ALL
SELECT 2, 'Alice, Apple,Barry, Banana,Claire, Cherry' FROM DUAL

输出

ID | PAIR           
-: | :--------------
 1 | Kanodia, Gaurav
 2 | Alice, Apple   
 1 | Punani, Rohit  
 2 | Barry, Banana  
 1 | Singhal, Bhavya
 2 | Claire, Cherry 

db <>提琴here