1

I have this query that have to select all books filtering by a description ignoring uppercase/lowercase.

So I make this query in adonis.js / node.js:

 const queryBook = Book .query() .with('user') queryBook.where('description', 'like', '%'+bookDescription[0]+'%') 

I have records with this bookDescription:

"Espanhol for Students ed.1 "

But when I try to filter using only "es" in lowercase, the knex don't return any record.

When I put "Es", return the book with the description that I put, so, the like %es% is not working.

I put one debug and I catch this:

knex:query select * from "books" where "description" like ? limit ? undefined +7ms knex:bindings [ '%es%', 10 ] undefined +6ms 

Apparently I don't find any wrong, but I think the like must return the record in lowercase..

I'm forgetting something?

6
  • 1
    which DB are you using? Postgres or mysql? Commented Nov 4, 2019 at 13:49
  • @Pavan i'm using postgres Commented Nov 4, 2019 at 14:00
  • Use ilike for postgres case insesntive matching Commented Nov 4, 2019 at 14:02
  • i also tried with ilike: queryBook.where('description', 'ilike', bookDescription[0]) but i'm still receiving no records Commented Nov 4, 2019 at 14:04
  • can you run the query in your SQL client and see if it returns the results? Commented Nov 4, 2019 at 14:06

4 Answers 4

8

You can use like this

const queryBook = Book .query() .with('user') queryBook.where('description', 'like', `%${bookDescription[0]}%`) 

or

 const queryBook = Book .query() .with('user') queryBook.where('description', 'ilike', `%${bookDescription[0]}%`) 

More Info. view knexjs docuemnts

Sign up to request clarification or add additional context in comments.

Comments

4

For case insensitive search you can use following like query

const queryBook = Book .query() .with('user') queryBook.whereRaw(`LOWER(description) LIKE ?`, [`%${bookDescription[0]}%`]) 

Comments

0

if you are using latest knexjs (> 1.x.x) one can use whereIlike or orWhereILike

const result = await select('*').from('books').whereILike('description',`${'%'+searchterm+'%'}`) 

Comments

0

you can use this best way

knex.whereILike('name',`%${search.toLowerCase()}%`);

1 Comment

Welcome to Stackoverflow. Please, always explain your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.