如何在HQL中使用Datediff() [英] How can I use Datediff() in HQL

查看:760
本文介绍了如何在HQL中使用Datediff()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图弄清日期之间的差异.在我的SQL SERVER中,它工作正常:

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

但是在我的HQL查询中我没有得到它. Datefiff函数可在HQL查询中使用吗?是否有任何具有相同行为的功能?

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?

推荐答案

HQL不支持datediff,但是如果您仍要使用datediff,则应使用createNativeQuery()createSQLQuery()在sql中编写它.在您的示例中,无论如何您只需要ID,而不是实体对象,因此就足够了.

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.

或者您可以在hql中使用类似的方法,但是如果您记录日期时间(不仅是日期),那么操作就有些棘手了.

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是1天的小时数,1小时的分钟数,1分钟的秒数和10是我认为的精度(1/10s),如果我不这样做,我会得到6天不同的69.44444444444445加上10并在末尾加上-1

/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

这篇关于如何在HQL中使用Datediff()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆