I am trying to get table data as list using select query between dateFrom and current date using MySQL Native Query in Spring Boot. and MySQL database field datatype is String.

Below is Query in Repository:

@Query(value = "select * from Account where DATETIMESTAMP >= :dateFrom AND DATETIMESTAMP < :DATE_FORMAT(curdate(), '%d/%m/%Y')", nativeQuery = true)List<Account> findByDate(@Param("dateFrom") String dateFrom);

Getting below error for above query in Spring Boot:

 Named parameter not bound : DATE_FORMAT; nested exception is org.hibernate.QueryException: Named parameter not bound : DATE_FORMAT

Can anyone please help me to frame the query for the same.

5

Best Answer


Remove the : from :DATE_FORMAT(curdate(), '%d/%m/%Y'). : is used for binding parameters in jpa query. The addition : in front of DATE_FORMAT makes JPA think it as a parameter.So the final query should be

@Query(value = "select * from Account where DATETIMESTAMP >= :dateFrom AND DATETIMESTAMP < DATE_FORMAT(curdate(), '%d/%m/%Y')", nativeQuery = true)List<Account> findByDate(@Param("dateFrom") String dateFrom);

The 'named parameter not bound' error usually occurs when you are trying to access a parameter that is not properly bound in your code. This error can be fixed by making sure that all your parameters are correctly bound and assigned values. In this article, we will discuss some common causes of this error and how to resolve it.

One possible cause of the 'named parameter not bound' error is when you are using a named parameter in your code, but you forgot to assign a value to it. To fix this error, you need to check all your named parameters and make sure that they have valid values assigned to them.

Another cause of this error is when you are trying to access a parameter that is not bound in your code. This can happen if you misspell the parameter name or if you forgot to declare and bind it properly. To fix this error, you need to review your code and make sure that all your parameters are properly declared and bound.

In conclusion, the 'named parameter not bound' error can be fixed by checking and correcting the assignment and binding of your parameters in your code. By making sure that all your parameters have valid values assigned to them and are properly bound, you can avoid this error and ensure the smooth execution of your code.

In any case, If you face this problem. Before doing debugging or deep google search Check the following things

  1. Method variable names and your query variables are matched or not.

  2. Query semicolon with your parameters

  3. If you renamed the table with short expressions, double-check them they were used correctly.

  4. Check your query syntax.

Because most of the issues have come from the above minor mistakes.

If you're using JPQL & still getting this error then its possible you might have a space in between like

where id = : myId

Should be

where id = :myId

I had the same error, but in my case I had a comparison operator '<>' joined on to my variable with no space in between. Adding a space between the variable and the operator fixed my issue.ie. changing :addressType<> to :addressType <> worked