I have a table called “Task” which has, among others, two columns:
- Date_due: a TIMESTAMP date
- Wait_in_seconds: a number (INT) of seconds
I want to add the Wait_in_seconds value to the date in the query and compare it to the current date.
The query below doesn’t have the right syntax, but it indicates what I want:
SELECT t
FROM Task t
WHERE (Date_due + Wait_in_seconds) < CURRENT_TIMESTAMP
Edit: KIMI explains that this is not possible with JPQL.
Edit 2: This native MySQL query does the trick:
SELECT *, DATE_ADD(task.date_due, INTERVAL task.wait_seconds SECOND) AS dueDate
FROM task
HAVING dueDate < NOW()
ORDER BY dueDate ASC
Standard JPQL does not support such operations on dates. You will have to either use a native query or do the computation on the Java side, for example with JodaTime which is a reference implementation of JSR-310.