How to make LIKE clause case-insensitive?

前端 未结 2 434
悲&欢浪女
悲&欢浪女 2020-12-16 03:29

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 \"Mor

相关标签:
2条回答
  • 2020-12-16 03:34

    try something like this

    def query_job(query)
      job_query = "%#{query.downcase}%"
      Job.where("lower(title) LIKE ? or lower(duration) LIKE ?", job_query, job_query) 
    end 
    
    query_job(params[:search])
    
    0 讨论(0)
  • 2020-12-16 03:42

    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]}%")
    
    0 讨论(0)
提交回复
热议问题