Postgres - WHERE clause on whether values ​​contain a specific string

If I want to get all records so that the column value foocontains a row 'bar', is there an easy way to do this in SQL or Postgresql?

Something like ' WHERE foo = "bar"', but instead =it will be something like ' WHERE foo CONTAINS "bar"'.

Postgres Version 9.3

+5
source share
2 answers

Use the SQL statement LIKE. In doing so, you use %as a wildcard. So your statement WHEREmight look something like this:

WHERE foo LIKE '%bar%'
+6
source

One of:

  1. WHERE foo LIKE '%bar%'

  2. WHERE foo ILIKE '%bar%' --case insensitive

  3. WHERE foo ~* 'bar' --regex

  4. WHERE foo ~* '\ybar\y' --matches bar but not foobar (\y is word boundary)

  5. WHERE foo::tsvector @@ 'bar'::tsquery --matches bar but not foobar

foo LIKE, ILIKE regexp: http://www.postgresql.org/docs/9.3/static/pgtrgm.html#AEN154464 GIN GIST

+7

Source: https://habr.com/ru/post/1589245/


All Articles