-->
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.  [ 3 posts ] 
Author Message
 Post subject: how to specify date in HQL where clause
PostPosted: Tue Jan 10, 2012 8:48 pm 
Newbie

Joined: Tue Jan 10, 2012 8:29 pm
Posts: 6
Hi I have a HQL query like this

Code:
   Date now = new Date();
       Date prevoius = now-interval;
       SimpleDateFormat Sformat = new SimpleDateFormat("dd-MMM-yy");
       String format_datenow = Sformat.format(now);
       String format_dateprevious = Sformat.format(prevoius);
      String markerCalcQuery = "select sum(trans_cnt) as t_cnt, location from map2_data where fdate between '"+format_dateprevious+"' and '"+format_datenow+"' and res_id = "+res_id+"  group by map2_data.location";
       res_row=gurculsql.rows(markerCalcQuery);


Right now if back-end is oracle I have to use the format dd-MMM-yy and if it is MySQL I use the format yyyy-MM-dd in the SimpleDateFormat. can this format be not specified when we use the date field in where clause and, be handled by Hibernate so that same format/code is used for both oracle and mysql.


Top
 Profile  
 
 Post subject: Re: how to specify date in HQL where clause
PostPosted: Wed Jan 11, 2012 6:25 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The recommended approach is to use a query with parameter placeholders (select .... where fdate between :previous and :now ...) and then set the actual values on the Query object before executing the query. For example query.setDate("now", now). Then you don't have to mess with formatting the dates at all in your code.


Top
 Profile  
 
 Post subject: Re: how to specify date in HQL where clause
PostPosted: Thu Jan 12, 2012 9:27 pm 
Newbie

Joined: Tue Jan 10, 2012 8:29 pm
Posts: 6
Thanks for the tip..!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.