harshkumar satapara
harshkumar satapara

Reputation: 75

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(i.e 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(i.e 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(i.e 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

Upvotes: 0

Views: 503

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269773

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.

Upvotes: 1

Related Questions