Find strings that have a field that is a substring of a search string

Based on the table:

  id |  value
 -------------------
 1 |  food
 2 |  foot
 3 |  barfoo
 4 |  bar
 5 |  baz

Using postgres, I want to find all the lines in which the value field matches the beginning of the search field. Similar to SELECT * FROM table where 'foo' ilike value%

A search for "foo" will return food and foot, but not barfoo.

I think it should be easy, but I am missing something obvious.

+4
source share
2 answers

do not switch comparison

 where value ilike 'foo%' 

Edit

  • Changed to the case of numbness "ilike", in the original example.

There are so many SQL dialects, so there is little storage space for greymatter.

+3
source

You have ILIKE arguments in the wrong way:

 SELECT * FROM table where value ilike 'foo%' 
0
source

All Articles