Date Compare in HQL without timestamp

后端 未结 2 1935
一向
一向 2021-02-14 02:58

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 Ta         


        
2条回答
  •  误落风尘
    2021-02-14 03:10

    The proposed solution with the date() function does not seem to be pure SQL and does not work for me (using SQL Server). The original solution (comparing day, month and year individually) is a step in the right direction, but cannot be done in this way, as, e.g., for a date to be in the future, a day need not be greater if the month is greater, etc. The following SQL, however, should work:

    ( year(t.endDate) > year(t.startDate) )
    or 
    ( year(t.endDate) = year(t.startDate) 
      and 
      month(t.endDate) > month(t.startDate) )
    or 
    ( year(t.endDate) = year(t.startDate) 
      and
      month(t.endDate) = month(t.startDate) 
      and
      day(t.endDate) > day(t.startDate) )
    

提交回复
热议问题