How can I use Datediff() in HQL

99封情书 提交于 2019-12-20 04:24:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!