skip to Main Content

I have a table where in a column there are titles. I would like to run a query in PostgreSQL, which helps me to select those titles that contain a particular word. (e.g. select every title that contains ‘You’, so it would give back I Love You, You Are The Best, How Are You?).

I have tried to use LIKE:

SELECT *
FROM table
WHERE title LIKE ('You');

But it still gives back zero result, because it only accepts exact matches:

SELECT *
FROM table
WHERE title LIKE ('I Love You');

Could you please help me how can I solve this issue?

Thank you!

2

Answers


  1. You need to use wildcards:

    SELECT * FROM table WHERE title LIKE ('%You%');
    
    Login or Signup to reply.
  2. The following query will perform a case insensitive search for titles that contain the word "You" but not words that just contain "you" somewhere within them:

    SELECT *
      FROM table
      WHERE title ~* '(^|W)You(W|$)';
    

    The regular expression W matches non-word characters. The expression (^|W) matches either the beginning of the text or the a non-word character; likewise, `(W|$)’ matches either a non-word character of the end of the text.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search