I know I can write a query that will return all rows that contain any number of values in a given column, like so:
Select * from tbl where my_col in (val1, val2, val3,... valn)
but if val1
, for example, can appear anywhere in my_col
, which has datatype varchar(300), I might instead write:
select * from tbl where my_col LIKE '%val1%'
Is there a way of combing these two techniques. I need to search for some 30 possible values that may appear anywhere in the free-form text of the column.
Combining these two statements in the following ways does not seem to work:
select * from tbl where my_col LIKE ('%val1%', '%val2%', 'val3%',....)
select * from tbl where my_col in ('%val1%', '%val2%', 'val3%',....)
Yes, you can use this query:
This way you don't have to create a table.
No, you cannot do this. The values in the IN clause must be exact matches. You could modify the select thusly:
If the val1, val2, val3... are similar enough, you might be able to use regular expressions in the REGEXP_LIKE operator.
Just to add on @Lukas Eder answer.
An improvement to avoid creating tables and inserting values (we could use
select from dual
andunpivot
to achieve the same result "on the fly"):But beware, that might be quite slow... Alternatively, you could insert all acceptable values (including
%
signs) into a table and semi-join that table:For true full-text search, you might want to look at Oracle Text:
http://www.oracle.com/technetwork/database/enterprise-edition/index-098492.html
This one is pretty fast :