views:

1827

answers:

2

I'm trying to group table values by date, and this HQL query works fine:

 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:

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, but I read it on Java's Hibernate forum.

So, how can I remove time part from this date, so that grouping works correct ?

Thanks in advance, Dejan.

+1  A: 

Hi!

You could use one of the built in functions in hql see mssql2000dialect

In your case Date seems to be the one

cws
Thanks for the answer. I'm using MS SQL 2005 dialect, but can you please write an example how can I use this, since, I'm newbie on this one. Thanks.
Dejan
The reason why I asked for this is because 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.
Dejan
A: 

I created a Scalar-Valued function inside the SQL Server 2005, which encapsulates this command:

CONVERT(VARCHAR(10), af.SubmitedDate, 105)

And inside the XML definition I placed this:

<property name='SubmitedDateWithoutTime' formula='dbo.RemoveTimeFromDateTime(SubmitedDate)'/>

So, my HQL query looks at the end like this:

SELECT  af.SubmitedDateWithoutTime, COUNT (af.Id) 
FROM ApplicationForm af 
GROUP BY af.SubmitedDateWithoutTime

and this works flawlessly.

Dejan