21

I'm trying to compose a WHERE statement that will match rows where a column value is a substring of another string.

For example, I might have an event record with a name field of Edward Sharpe. I'd like to do something like:

SELECT * FROM events WHERE(name LIKE 'Edward Sharpe and the Magnetic Zeroes');

This doesn't work. I've also various permutations of:

SELECT * FROM events WHERE('%' || name || '%' LIKE 'Edward Sharpe and the Magnetic Zeroes');

Which also doesn't work.

4
  • Have you considered regexp_matches? Commented Jun 26, 2014 at 22:44
  • Is it select 'Edward Sharpe and the Magnetic Zeroes' like '%' || name || '%' from (select 'Edward Sharpe'::text "name") foo? Commented Jun 26, 2014 at 22:51
  • @mlt No, that query makes no sense. As you've written it there, it will return a single unnamed column with the value true, if I'm not mistaken. Commented Jun 26, 2014 at 23:09
  • 1
    @mlt But what's with the extra sub-query, and not mentioning any actual table? It's a valid query, but not very similar to what was asked for. Commented Jun 26, 2014 at 23:17

1 Answer 1

33

Your second attempt is painfully close to correct. The LIKE keyword takes a string on its left, and a pattern on its right. Both can be expressions, but % only has a special meaning in the pattern to the right.

Try this:

 SELECT * FROM events
 WHERE name LIKE '%Edward Sharpe and the Magnetic Zeroes%';

Or rather this:

 SELECT * FROM events 
 WHERE 'Edward Sharpe and the Magnetic Zeroes' LIKE '%' || name || '%';

Also note that all string operations in Postgres are case sensitive by default. To match a pattern ignoring case, use ILIKE in place of LIKE.

Sign up to request clarification or add additional context in comments.

6 Comments

Your second example did it. Apparently the wildcard-ed value has to come after the LIKE statement. Thank you!!
I just tweaked my answer because WHERE doesn't need any brackets for a simple case like this. They won't hurt, just a waste of keystrokes.
Some typos. And the first query is off target AFAICT.
@ErwinBrandstetter Feel free to edit for typos, I'm on a mobile, so positioning accurately is tricky. I gave both queries to stress that the %s can go onto either value, as long as they're on the right of the LIKE
Well, strictly speaking you are right. The actual operator behind the scenes is ~~ or ~~* for ILIKE. So I switched back to "keyword". Careful, btw, the otherwise equivalent operator ~~ is ranked differently in operator precedence. You would have to use parentheses: ... ~~ ('%' || name || '%').
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.