-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How to use Oracle TO_DATE syntax in named query
PostPosted: Thu Jun 30, 2005 4:11 pm 
Newbie

Joined: Thu Jun 30, 2005 3:57 pm
Posts: 2
Hello,

I have a problem that I know someone else has solved before, but I just can't find the solution myself.

We're using Oracle 9i and named queries in Hibernate 2.1.7. One of the database column's type is a datetime, but we'd like to be able to force the where clause to use a TO_DATE style formatting mechanism to compare based on MM/DD/YYYY.

The query snippet is as follows:

Code:
select payrollWeek
from PayrollWeek pw
where pw.record_date = :recDate


I can trim :recDate in my code, but pw.record_date in some cases will have timestamp information, and in others it won't, so I'd like to discard that the way you would in Oracle:

Code:
select columns
from PayrollWeek
where to_date(record_date, 'MM/DD/YYYY') = :recDate


I apologize if this is a trivial problem, but I did search the documentation and have come up empty-handed.

Thanks for any and all responses,
John


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 5:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
http://www.hibernate.org/hib_docs/v3/re ... yhql-where

You can use SQL functions in HQL.


Top
 Profile  
 
 Post subject: to_date to work in both Oracle and DB2?
PostPosted: Fri Jan 16, 2009 5:13 am 
Newbie

Joined: Tue Nov 18, 2008 4:12 am
Posts: 8
Location: Singapore
Hi,

Is there a HQL implementation of the to_date function which will be translated into the corresponding syntax on different database types? In my named query I have a to_date() function from Oracle which I want to work on DB2 as well.

_________________
Life at i-flex turns me on!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 5:36 am 
Newbie

Joined: Thu Jan 15, 2009 9:54 am
Posts: 12
I m not sure whether i got your question or not.
Apologies if this is not what u r looking for.

you can try formula of the property element.
<property name="stringDate" formula="to_date(recDate,'MM/DD/YY') "

you ll then get a String object of Date in mm/dd/yy format.
you can then change it to Date format using SimpleDateFormat.

then u can compare two different dates.


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 5:56 am 
Newbie

Joined: Tue Nov 18, 2008 4:12 am
Posts: 8
Location: Singapore
Thanks for the tip!

Actually I'm trying to write a HQL query here which can convert a string parameter into a date, and then do a row insertion on either Oracle or DB2 environment.

On Oracle SQL, I'll write the query as "insert into mytable values (TO_DATE(:dateVar,'YYYY-MM-DD HH24:MI:SS'))"

On DB2 SQL, I'll write the query as "insert into mytable values (TIMESTAMP_FORMAT(:dateVar, 'YYYY-MM-DD HH24:MI:SS'))"

I'm looking for a HQL function that will be converted to the corresponding Oracle or DB2 syntax, depending on the dialect used.

Thanks,
Alvin

_________________
Life at i-flex turns me on!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.