2

I have a named query to search for customer name as shown below. This is currently matching all the customers whose names have the specified search criteria in any part of their name (equivalent to %search%).

"SELECT t FROM Customer t WHERE t.name like :search"

How do i specify the named query so that it matches only the start of the name with the criteria specified using named queries?

Regards,

user1082762
  • 107
  • 1
  • 5

2 Answers2

3

You can do it this way. Just append the % symbol to the start or end(depending on your requirements) of the keyword to be searched.

query.setParameter("search", searchKeyword+"%"); // Searches for words starting with searchKeyword
Rahul
  • 44,383
  • 11
  • 84
  • 103
1
SELECT t FROM Customer t WHERE t.name like :search

and set the Parameter as

query.setParameter("search", value + "%" )
Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
Siva
  • 1,938
  • 1
  • 17
  • 36