Search code examples
javajpaspring-data-jpajpql

Error Unexpected token '%' : JPQL - LIKE %FUNCTION(:namedParam)%


I get Unexpected token '%' when use this JPQL query: In my case I use the upper function but it could be anyone.

@Query("SELECT s FROM Entity s WHERE UPPER(s.asunto) LIKE %UPPER(:asunto)% ")
public Entity namedMethod(@Param("asunto") String asunto));

I need to add LIKE %:param% to find any coincidence of the param inside my text.

Have tried :

@Query("SELECT s FROM Entity s WHERE UPPER(s.asunto) LIKE '%'UPPER(:asunto)'%' ")

Unexpected token: UPPER

@Query("SELECT s FROM Entity s WHERE UPPER(s.asunto) LIKE '%UPPER(:asunto)%' ")

Unexpected token: UPPER

No luck in there either.

Thanks in advance.


Solution

  • Solved, there are two ways to resolve this problem:

    @Query("SELECT s FROM Entity s WHERE UPPER(s.asunto) LIKE '%' || UPPER(:asunto) || '%' ")
    
    @Query("SELECT s FROM Entity s WHERE UPPER(s.asunto) LIKE CONCAT('%', CONCAT(UPPER(:asunto),'%')) ")
    EDIT-- Thanks to @Nicolau: 
    @Query("SELECT s FROM Entity s WHERE UPPER(s.asunto) LIKE UPPER(CONCAT('%',:asunto,'%'))")
    

    Both works!

    Thanks to @M.Prokhorov.