user3426001
user3426001

Reputation: 1

Can I use a table column name as a search argument in db2 "like" or "contain" operator/function

I have two tables: one containing a column (Description) that I would like to search using values in a column (keyword) in another table. I would like to perform something like

select table1.description, tabl2.keyword,
case when
table1.description like `'''%'||table2.keyword||'%''' then 1
-- or contains(table1.description, table2.keyword)
else 
0
end
as found
from table1, table2

The documentation for Contain function seems to indicate that the search parameter (table2.keyword in my case) has to be a constant (I suspect "like" also has similar constraints).

The error messages I get seem to indicate this constraint.

Is there a solution that I can use?

Upvotes: 0

Views: 101

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269643

You can do it with like. Your query should work:

select table1.description, tabl2.keyword,
       (case when table1.description like '''%'||table2.keyword||'%'''
             then 1 else 0
        end) as found
from table1 cross join table2;

I prefer explicit joins, cross join instead of ,.

Upvotes: 1

Related Questions