69

I want to search data in User table by name case insensitive.

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

  @Query("select u from User u where lower(u.name) like %lower(?1)%")
  public List<User> findByNameFree(String name);

}

I got an error: unexpected token: %. Where should I place '%'?

6 Answers 6

145

You can use the concat operator:

@Query("select u from User u where lower(u.name) like lower(concat('%', ?1,'%'))")
public List<User> findByNameFree(String name);

or with a named parameter:

@Query("select u from User u where lower(u.name) like lower(concat('%', :nameToFind,'%'))")
public List<User> findByNameFree(@Param("nameToFind") String name);

(Tested with Spring Boot 1.4.3)

6
  • Good approach. Thank you! Jan 15, 2020 at 15:04
  • Note that this might change your query plan and bypass a SQL index, slowing down queries, especially with lots of users in the table. This is because it has to apply the lower() to all the values in the DB on every call.
    – Max
    Jul 6, 2020 at 11:50
  • @Max Thanks for that observation, did not think of that. According to stackoverflow.com/questions/12855/… there are ways to avoid it, but it very much seems to depend on what db you are using. Jul 6, 2020 at 12:21
  • when given null, boom baby!
    – withoutOne
    Jul 27, 2020 at 12:01
  • it fails when nameToFind is null. any solution? Sep 28, 2020 at 5:18
41

If that is only what you want and you are using Spring Data JPA you don't need to write a query.

List<User> findByNameContainingIgnoreCase(String name);

Else you need to wrap the name attribute with % before you pass it to the method (putting those directly in the query will simply not work). Or don't use a query but use a specification or the Criteria API to create the query.

2
  • 1
    I want to find solution for general problem that use query. May 17, 2016 at 9:27
  • doesnt work... the value is not put to lowercase Sep 1, 2022 at 15:40
17

I am using Spring Boot 2.1.6, You can define query methods using Containing, Contains, and IsContaining as below:

List<User> findByNameContaining(String name);
List<User> findByNameContains(String name);
List<User> findByNameIsContaining(String name);

Case Insensitivity:

List<User> findByNameContainingIgnoreCase(String name);

OR you can also define as below as well:

@Query("select u from User u where lower(u.name) like lower(concat('%', :name,'%'))")
public List<User> findByName(@Param("name") String name);

The @Param annotation is important here because we're using a named parameter.

1
  • Does the concat result in %name% being the search term? What do the % do?
    – Al Grant
    Oct 8 at 22:38
3

Without using concat and using TypedQuery:

  TypedQuery<Baptism> query = entityManager.createQuery("SELECT d FROM " + Baptism.class.getSimpleName()
                            + " d JOIN d.person p WHERE UPPER(p.lastName) LIKE UPPER(:ln)", Baptism.class);
                    query.setParameter("ln", "%" + ln + "%");
0

You can use spring boot naming convention strategy to achieve with naming pattern like: findByYourFieldCamelCaseLikeIgnoreCase

example: findByUserNameLikeIgnoreCase(userName: String): List<UserEntity>

-1

You can use wildcard matching.

for example, i want to search name like haha,

@Query("select u from User u where lower(u.name) like :u_name")
public List<User> findByNameFree(@Param("u_name") String name);
List<User> users = userDao.findByNameFree("%haha");
1
  • 4
    @Query("select u from User u where lower(u.name) like lower(:u_name)") May 12, 2016 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.