如何有效地搜索Oracle中的分隔字符串?

时间:2016-08-04 14:28:13

标签: oracle

我有一个Oracle 11g表设置为:

CODE   VERSION   STRING_DATA 
AAA    1         aaa|bbb|EARTH|ddd|eee
AAA    1         aaa|bbb|MARS|ddd|eee
AAA    2         aaa|b22|EARTH|d22|eee
BBB    1         aaa|b22|EARTH|d22|eee

现在STRING_DATA被细分为:

  1. col1
  2. col2
  3. location
  4. col4
  5. COL5
  6. 我怎么能写一个类似于我的伪代码的查询?

    SELECT *
    FROM MY_TABLE
    WHERE CODE = 'AAA' AND VERION = 1 AND LOCATION = 'EARTH
    

2 个答案:

答案 0 :(得分:2)

使用Substr和Instr

   select * 
     from MY_Table
    where CODE = 'AAA' 
       AND VERION = 1 
       AND substr(string_data,instr(string_data,'|',1,2)+1, instr(string_data,'|',1,3)-instr(string_data,'|',1,2)-1)  = 'EARTH';

答案 1 :(得分:0)

with
     my_table ( code, version, string_data ) as (
       select 'AAA', 1, 'aaa|bbb|EARTH|ddd|eee' from dual union all
       select 'AAA', 1, 'aaa|bbb|MARS|ddd|eee'  from dual union all
       select 'AAA', 2, 'aaa|b22|EARTH|d22|eee' from dual union all
       select 'BBB', 1, 'aaa|b22|EARTH|d22|eee' from dual
     )
select *
from my_table
where code = 'AAA' and version = 1 and regexp_like(string_data, '^[^|]*\|[^|]*\|EARTH\|')
;
COD    VERSION STRING_DATA
--- ---------- ---------------------
AAA          1 aaa|bbb|EARTH|ddd|eee