我需要从MySQL表中获取一些记录,其中列posted_date
为Timestamp
。我需要通过比较月份来过滤记录。例如,我想要本月发布的所有记录。
我试过这个链接Hibernate query date compare on year and month only
但我得到以下错误
line 1:106: unexpected token: to_char
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:769)
如何通过将月份作为参数来比较HQL中的月份?
答案 0 :(得分:4)
使用MONTH(...)HQL函数,如下所示:
select so from SomeObject so where MONTH(so.date) = MONTH(:date)
或
select so from SomeObject so where TO_CHAR(so.date, 'MM') = :month_as_string