-->
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: HOUR() and MINUTE() functions in Native SQL
PostPosted: Thu Aug 09, 2012 9:34 am 
Newbie

Joined: Wed Aug 01, 2012 5:51 am
Posts: 8
Hi !

I need to get the hour and minute from dates and I'm using Native SQL, MONTH() and DAY() work perfectly, but I get an error if I change them to HOUR() or MINUTE()

Do you know a way to get them ?

thanks


Top
 Profile  
 
 Post subject: Re: HOUR() and MINUTE() functions in Native SQL
PostPosted: Thu Aug 09, 2012 9:46 pm 
Newbie

Joined: Tue Aug 07, 2012 4:58 am
Posts: 4
hi, jah_404

if your database is Oracle, you can try this:

SELECT EXTRACT(HOUR FROM TO_TIMESTAMP('01-1月-2005 19:15:26', 'DD-MON-YYYY HH24:MI:SS')) AS HOUR,
EXTRACT(MINUTE FROM TO_TIMESTAMP('01-1月-2005 19:15:26', 'DD-MON-YYYY HH24:MI:SS')) AS MINUTE,
EXTRACT(SECOND FROM TO_TIMESTAMP('01-1月-2005 19:15:26', 'DD-MON-YYYY HH24:MI:SS')) AS SECOND
FROM dual;

it works in my local.

best wishes.


Top
 Profile  
 
 Post subject: Re: HOUR() and MINUTE() functions in Native SQL
PostPosted: Fri Aug 10, 2012 3:03 am 
Newbie

Joined: Wed Aug 01, 2012 5:51 am
Posts: 8
Thank you for your answer Kenny_Zhang !
unfortunately I'm using SQL Server and it doesn't seem to work


Top
 Profile  
 
 Post subject: Re: HOUR() and MINUTE() functions in Native SQL
PostPosted: Fri Aug 10, 2012 3:53 am 
Newbie

Joined: Wed Aug 01, 2012 5:51 am
Posts: 8
Thank to your post, I realized that using native sql request depends on the DBMS
As I'm using SQL Server, the way to get the hour of a date is :
Code:
session = HibernateUtil.currentSession();

String sql_query = "SELECT" +
      " DATEPART(hh,DH_departTrain) AS hour," +
      " Id" +
      " FROM Circulation" +
      " WHERE Id = 1722";

@SuppressWarnings("unchecked")
Query query = session.createSQLQuery(sql_query)
.addScalar("hour", Hibernate.INTEGER)
.addScalar("Id", Hibernate.INTEGER);


Top
 Profile  
 
 Post subject: Re: HOUR() and MINUTE() functions in Native SQL
PostPosted: Fri Aug 10, 2012 7:09 am 
Newbie

Joined: Tue Aug 07, 2012 4:58 am
Posts: 4
jah_404 wrote:
Thank to your post, I realized that using native sql request depends on the DBMS
As I'm using SQL Server, the way to get the hour of a date is :
Code:
session = HibernateUtil.currentSession();

String sql_query = "SELECT" +
      " DATEPART(hh,DH_departTrain) AS hour," +
      " Id" +
      " FROM Circulation" +
      " WHERE Id = 1722";

@SuppressWarnings("unchecked")
Query query = session.createSQLQuery(sql_query)
.addScalar("hour", Hibernate.INTEGER)
.addScalar("Id", Hibernate.INTEGER);


hi, Jah_404

yes, you can use DATEPART function to get the hour of a date, if you are using SQL Server.

but I think this is not a good thing, if you migrate your data from SQL Server to Oracle one day.

so, maybe you should figure out another solution that is Independent of DBMS.


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.