persistence@glassfish.java.net

Re: how to compare 2 dates in a JPA QL query

From: Wouter van Reeven <wouter_at_van.reeven.nl>
Date: Wed, 13 Aug 2008 11:42:05 +0200

On Wed, Aug 13, 2008 at 02:30:54AM -0700, chflb wrote:
> how can i compare 2 dates in JPA QL ,?
>
> my idea was to convert the dates to String, and i used 'to_char',
> but i think it doesn't work in JPA QL;
> this is my query(it works in oracle but not in java) :
>
> Query q=em.createQuery("select sum(o.montant) from Operation o join
> o.codTransac t join o.codCaisse c where (t.sens='D')and
> (to_char(c.dateCaisse,'MM/DD/YYYY')=to_char(o.dateOp,'MM/DD/YYYY'))");

In JPA QL, dates are just Date objects. So this should work

Query q=em.createQuery("select sum(o.montant) from Operation o join
o.codTransac t join o.codCaisse c where (t.sens='D')and
(c.dateCaisse=o.dateOp)");


HTH, Wouter van Reeven

-- 
People: "If she weighs the same as a Duck, she's made of wood!"
Sir Bedevere: "And therefore...?"