1

I'm struggling in creating a dynamic query with null check on date in spel using spring data. My query is:

 @Query(nativeQuery = true, value = "select a.* from TABLE a where"
        + "(:#{#DateFrom} is null or a.F_DATE >= trunc(:#{#DateFrom}))")
List<Acc> getAccList(@Param("DateFrom") @Temporal(TemporalType.DATE) Date datefrom);

When I run this query with valid date it works, but when I pass null date I get following error:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

Stack trace:

Caused by: java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected NUMBER got BINARY

at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:205)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:861)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1145)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1267)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3493)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
at com.ing.cbp.commons.util.logging.sql.LogablePreparedStatement.executeQuery(LogablePreparedStatement.java:77)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
... 78 common frames omitted

2 Answers 2

1

You need add one whitespace after 'where' keyword.

@Query(nativeQuery = true, value = "select a.* from TABLE a where "
    + "(:#{#DateFrom} is null or a.F_DATE >= trunc(:#{#DateFrom}))")
List<Acc> getAccList(@Param("DateFrom") @Temporal(TemporalType.DATE) Date datefrom);
Sign up to request clarification or add additional context in comments.

Comments

0

It seems oracle considers the NULL value a binary and than fails when it tries to perform trunc on it. Casting it to a Date should help. So the resulting query should look like this:

select a.* from TABLE a where (:#{#DateFrom} is null 
or a.F_DATE >= trunc(CAST(:#{#DateFrom} AS DATE))

2 Comments

Tried it, got same exception :(
Can you remove one part of the condition at a time in order to determine which one is causing the problem? Also please log the SQL statement that actually gets executed. Finally, can you execute the SQL statement with an actual null literal instead of the bind variable?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.