Search code examples
sqlpostgresqlwhere-clausesql-like

How do you search for a pattern and ignore case in PostgreSQL?


How would I make it so that this query is case insensitive so it finds instances of "Hop" and "hop" even when the search query is only "hop"

  const {
    rows,
  } = await db.query(
    "SELECT * FROM course WHERE header LIKE '%' || $1 || '%'",
    [req.body.searchbar]
  );

Solution

  • Use ILIKE:

    SELECT * FROM course WHERE header ILIKE '%' || $1 || '%'
    

    You can also express this with the ~~* operator:

    SELECT * FROM course WHERE header ~~* '%' || $1 || '%'