Kafka Tamura
Kafka Tamura

Reputation: 79

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]
  );

Upvotes: 1

Views: 88

Answers (1)

GMB
GMB

Reputation: 222462

Use ILIKE:

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

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

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

Upvotes: 1

Related Questions