4

Below is the simple example which explain my problem

Query(name = "select p.* , pr.actual_date_time , count(*) " +
"from player p  " +
"inner join app.player_reports pr ON pr.player_id = p.id " +
"where pr.actual_date_time between  now() - interval '?1 day' 
   and now() - interval '0 day' " ,nativeQuery = true)
List<PlayerEntity> findCheaters(@param("NumberDays") int number )

this is my query, I want to put "NumberDays" between single quotation and substitute with "?1".

I would be glad to help me, and thank you

4
  • You can't use a parameter inside interval I believe. Commented Aug 21, 2018 at 6:05
  • do you have any idea how to do this query? Commented Aug 21, 2018 at 6:12
  • You would need to use a date function which accepts a pure numeric parameter. Then, we could make it work. Commented Aug 21, 2018 at 6:12
  • got it gonna try this way to fix it Commented Aug 21, 2018 at 6:15

1 Answer 1

2

You can't parametrize the value for the interval constant, but you can simply multiply the base unit with a parameter:

pr.actual_date_time between  now() - (interval '1 day' * ?) and now()

Then pass the number of days you want as an integer to the PreparedStatement.


Note that the - interval '0 day' is useless

Sign up to request clarification or add additional context in comments.

2 Comments

so I should pass the query a String ?
No, an integer: the number of days.

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.