Reputation: 3233
I have an Oracle 11g table setup as:
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
Now the STRING_DATA is broken down as:
How could I write a query similar to my psuedo-code?
SELECT *
FROM MY_TABLE
WHERE CODE = 'AAA' AND VERION = 1 AND LOCATION = 'EARTH
Upvotes: 0
Views: 26
Reputation:
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
Upvotes: 0
Reputation: 4757
Use Substr and 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';
Upvotes: 2