Insomania Insomania - 1 year ago 73
Ruby Question

How to make "Like Clause" Case-Insensitive

Hi I'm using a LIKE clause in Ruby On Rails,when I try to search for records by typing "more" it doesn't return anything, but when I do with "More", then it returns the records which contains More keyword, so it seems like it behaves Case-Sensitive,

Now my query is can we make it Case-Insensitive, if yes then please let me know.

here is the query I am using currently:

Job.where('title LIKE ? OR duration LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")


Answer Source

I assume you're using Postgres.

You can use ILIKE

Job.where('title ILIKE ? OR duration ILIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")

Or a some tricky hack lower():

Job.where('lower(title) LIKE lower(?) OR lower(duration) LIKE lower(?)', "%#{params[:search]}%", "%#{params[:search]}%")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download