Date Compare in HQL without timestamp

25,647

See the Hibernate documentations for available date functions

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

In section 14.10 notice this line

second(...), minute(...), hour(...), day(...), month(...), and year(...)

So this should work

... where year(t.endDate) > year(t.startDate) 
    and month(t.endDate) > month(t.startDate)
    and day(t.endDate) > day(t.startDate)

The solution reported as satisfying the question is

 ... where DATE(t.endDate) > (t.startDate)
Share:
25,647
Sanket Dosi
Author by

Sanket Dosi

Because knowledge is not for showing off. If I do good work, people should notice me - Chetan Bhagat

Updated on September 01, 2020

Comments

  • Sanket Dosi
    Sanket Dosi over 3 years

    I have to compare two dates in hibernate hql query. I am using java.util.Date in my java bean and using timestamp as datatype in mysql database.

    select t from Task t where t.modifiedDate > t.endDate;
    

    Above query compares time with date. What should i do to compare date in above query without time.

  • Sanket Dosi
    Sanket Dosi over 9 years
    This is giving me the comparison with month & year separately ... but i have to compare full date at a time like 06-09-2014
  • Sanket Dosi
    Sanket Dosi over 9 years
    It works with following change where DATE(t.endDate) > (t.startDate)
  • carbontax
    carbontax over 9 years
    Glad it worked for you. I am editing the answer with your solution rather than leave the solution in the comments. That is the Stackoverflow way of doing things.
  • dom
    dom about 6 years
    instead of t.startDate if we need to put a date as a name paramter then the query will be like, Date(t.endDate) <= (:toDate), this one not working for me any help
  • KJEjava48
    KJEjava48 over 4 years
    @carbontax Is it allowed to use "where DATE(t.endDate) > DATE(:currentDate) " in hql query.That is using DATE() with the input parameter??