Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Compare in HQL without timestamp

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.

like image 638
Sanket Dosi Avatar asked Sep 05 '14 06:09

Sanket Dosi


People also ask

How do you compare two dates without the time portion?

If you want to compare just the date part without considering time, you need to use DateFormat class to format the date into some format and then compare their String value. Alternatively, you can use joda-time which provides a class LocalDate, which represents a Date without time, similar to Java 8's LocalDate class.

How do I compare dates in HQL?

In Oracle You Can Use HQL trunc() Date Function Another function you can use for HQL date comparison queries is the HQL trunc() function.

How do you compare only dates?

CompareTo(DateOnly) Compares the value of this instance to a specified DateOnly value and returns an integer that indicates whether this instance is earlier than, the same as, or later than the specified DateOnly value.


1 Answers

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)
like image 122
carbontax Avatar answered Oct 15 '22 07:10

carbontax