Search code examples
oracle-databasecontains

How to allow search using CONTAINS on 'B&B' without getting the prompt for 'Enter Substitution Variable' because of the & and get only 'B&B'


I am performing a search of a CLOB that is Globally indexed so I can use a CONTAINS function. The phrase I am searching for is 'B&B'. I have tried multiple ways to escape the & so it won't be looked at as a prompt for user input. I am unable to do it.

select * from table where contains(txt, '({B&B})')>0;--this gives me substitution variable prompt

select * from table where contains(txt, '({B}{&}{B})')>0;--this finds 'B B'

select * from table where contains(txt, '(B{&}B)')>0;--this finds 'B B'

select * from table where contains(txt, '({B&B})')>0;--this gives me substitution variable prompt

select * from table where contains(txt, '({B&}B)')>0;--this finds 'B B', 'B.B', 'B&B'

select * from table where contains(txt,'NEAR (({B&},(B)),1)') > 0;--this finds 'B B', 'B.B', 'B&B'

select * from table where txt like '%B&B%';--this gives me substitution variable prompt

I can't deactivate the ability to use substitution variable prompt, so this must be done in code.

I need to ignore all occurrences of 'B B', 'B.B' and have it return only the rows that have 'B&B' in this field.


Solution

  • I know, it is a CLOB. Would INSTR do any good, anyway?

    SQL> create table test (txt clob);
    
    Table created.
    
    SQL> set define off
    SQL> insert into test
      2    select 'This is an example which does not contain BB, B B, B.B' from dual union all
      3    select 'Query should return B&B because it is here' from dual;
    
    2 rows created.
    
    SQL> select txt
      2  from test
      3  where instr(txt, 'B&B') > 0;
    
    TXT
    --------------------------------------------------------------------------------
    Query should return B&B because it is here
    
    SQL>