-->
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.  [ 2 posts ] 
Author Message
 Post subject: NHibernate HQL: Remove time part from date field
PostPosted: Sun Jul 05, 2009 3:41 am 
Newbie

Joined: Wed Jul 01, 2009 4:44 am
Posts: 2
Hi all,

I have a problem with date field. I searched topics here and couldn't find the answer,
I also posted a question on Stack Overflow, but still got no specific answer.

The problem is following:

I'm trying to group table values by date, and this HQL query works fine:
Code:
SELECT  af.SubmitedDate, COUNT (af.Id)
FROM ApplicationForm af
GROUP BY af.SubmitedDate

The problem is that field af.SubmitedDate also contains time part, sine I'm using SQL Server 2005, so the grouping is done by date-time, not only by date. When I try to do something like this in HQL:
Code:
SELECT CONVERT(VARCHAR(10), af.SubmitedDate, 105), COUNT (af.Id)
FROM ApplicationForm af
GROUP BY CONVERT(VARCHAR(10), af.SubmitedDate, 105)

...I receive this error:

NHibernate.QueryException was unhandled by user code
Message="undefined alias or unknown mapping: CONVERT

This query is correct in TSQL and I even read somewhere that CONVERT can be used in HQL, but I got this error message.

I got answer on Stack Overflow that I should use mssql2000 dialect.
I'm using mssql2005 dialect, but I'm guessing that I can apply built in function AFTER I get the result from DB,
or on some value that I will PASS to HQL query, which is not what I need here.
I need to remove time part from date field, WHILE the GROUP BY is performed,
so that my final outcome is records grouped by date only, not date-time.

Thanks in advance,
Dejan.


Top
 Profile  
 
 Post subject: Re: NHibernate HQL: Remove time part from date field
PostPosted: Mon Jul 06, 2009 6:39 am 
Newbie

Joined: Wed Jul 01, 2009 4:44 am
Posts: 2
I created a Scalar-Valued function inside the SQL Server 2005, which encapsulates this command:
Code:
CONVERT(VARCHAR(10), af.SubmitedDate, 105)

And inside the XML definition I placed this:
Code:
<property name='SubmitedDateWithoutTime' formula='dbo.RemoveTimeFromDateTime(SubmitedDate)'/>

So, my HQL query looks at the end like this:
Code:
SELECT  af.SubmitedDateWithoutTime, COUNT (af.Id)
FROM ApplicationForm af
GROUP BY af.SubmitedDateWithoutTime

and this works flawlessly.


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