简体   繁体   中英

Compare exact string in oracle sql

I have column in which different values placed with ':' colon separate. I want to match exact string in column. lets assume table is temp and column name is sample_col. order of values can also change.

sr_no|sample_col

1 |    'Sample-ABC'
2 |   'Sample-ABC-123'
3 |    'Sample-ABC-123:XYZ'
4 |    'Sample-ABC-123:Sample-ABC'
5 |    'XYZ:Sample-ABC'

So

1) require a query to search a records which is having value 'Sample-ABC' then 3 records(ie sr_no 1,4,5) should come through query

2) require a query to search a records which is having value 'Sample-ABC-123' then 3 records(ie sr_no 2,3,4) should fetch.

3) require solution for those records which are having 'Sample-ABC-123' values but not having 'Sample-ABC' then 2 records(ie sr_no 2,3) should fetch.

I tried to get output with regexp_like,instr,like etc option but failed. can anyone help me to sorting this.

below is sql query i tried fetch records which are having 'Sample-ABC' records:

select * from temp
where instr(sample_col,'Sample-ABC') > 1
and sample_col not like '%Sample-ABC-123%'; --this exclude 'Sample-ABC-123:Sample-ABC' record from

select * from temp
where sample_col not like '%Sample-ABC%'; --this will include 'Sample-ABC-123' records as well.


select * from temp
where regexp_like(sample_col,'Sample-ABC'); --this will include 'Sample-ABC-123' records as well.

I require query which will provide output with all the

You probably already know that this is a bad data format and you should be using a more appropriate structure to store multiple values, such as a table, nested table, JSON, XML or something else.

That said, you can do what you want by adding delimiters for the comparison:

where ':' || sample_col || ':' like '%:Sample-ABC:%'

This will find exact matches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM