从多个方括号中获取文本

问题描述 投票:-1回答:1

对于Oracle SQL

i具有以下标记格式:{ABCDE,12345} {47,CE}

SELECT 
[column a] 
[column b] 
[column c] 
FROM 
[table name] 
WHERE 
[*content of column d*] IN {ABCDE,12345} 
[*content of column e*] IN {47,CE}

所以我需要查询以查看标记中的内容,并在以下情况下选择所有具有相同内容的行:-D列=第一组括号-E列=第二组中的内容

sql oracle regexp-substr
1个回答
0
投票

一个选项可能是这个(请参阅代码中的注释):

SQL> with test (col) as
  2    (select '{ABCDE,12345}{47,CE}' from dual),
  3  temp as
  4    -- remove leading and trailing brackets
  5    -- replace "middle" brackets with semi-colon (a new separator)
  6    (select replace(substr(col, 2, length(col) - 2), '}{', ';') val
  7     from test
  8    )
  9  select regexp_substr(val, '[^;]+', 1, level) result
 10  from temp
 11  connect by level <= regexp_count(val, ';') + 1;

RESULT
--------------------------------------------------------------------
ABCDE,12345
47,CE

SQL>

啊,是的-单独的:那么,substr可能有帮助:

SQL> with test (col) as
  2    (select '{ABCDE,12345}{47,CE}' from dual)
  3  select substr(col, 2, instr(col, '}{') - 2) val1,
  4         substr(substr(col, instr(col, '}{') + 2),
  5                1,
  6                length(substr(col, instr(col, '}{') + 2)) - 1
  7               ) val2
  8  from test;

VAL1        VAL2
----------- -----
ABCDE,12345 47,CE

SQL>
© www.soinside.com 2019 - 2024. All rights reserved.