Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql Contains in where clause

Is there a function in postgres like contains ? that can be used in the where clause to check , whether the string passed is contained in column?

like image 950
Gopinagh.R Avatar asked Mar 13 '13 06:03

Gopinagh.R


2 Answers

You could use position() for that. It returns zero if the substring is not found:

position(col2 in col1) <> 0
like image 161
NPE Avatar answered Oct 13 '22 18:10

NPE


There are a bunch of ways of solving this:

  1. Use like, ilike, and/or SIMILAR TO along with ||. To handle columns, something like:

    WHERE col1 ilike '%' || col2 || '%';
    
  2. Use position as NPE's answer

  3. You could also use regexp_matches but that is more complex.

like image 20
Chris Travers Avatar answered Oct 13 '22 18:10

Chris Travers