问题
I'm trying to get the difference between dates. In my SQL SERVER it's works fine:
Select CSERVICE_ORDER_ID
FROM TSERVICE_ORDERS
WHERE DATEDIFF(DAY, CDB_CREATE_DATE_TIME, CCANCELLATION_DATE) = 4
But in my HQL query I'm not getting it. The function Datefiff works in HQL Query? Is there any function with the same behavior?
回答1:
HQL doesn't support datediff, but if you still want to use datediff, you should use createNativeQuery()
or createSQLQuery()
to write that in sql. In your example, you just need the id anyway, not entity object, so this should be enough.
or you can use in hql something like this, but the precission is a bit tricky if you record the datetime(not only date).
4>(((CDB_CREATE_DATE_TIME - CCANCELLATION_DATE)/24/60/60/10)-1)>3
/24/60/60/10 is hours in 1 day, minute in 1 hour, second in 1 minute and 10 is the precission i think (1/10s), cz i get 69.44444444444445 for 6 days different if i dont devide with 10 and put -1 at the end
来源:https://stackoverflow.com/questions/22881990/how-can-i-use-datediff-in-hql