Using SQL Functions in Criteria Restrictions

9 commentsWritten on April 30th, 2009 by
Categories: NHibernate

A coworker needed to use a SQL function in the where clause of a query that he was creating with NHibernate's ICriteria API. Most examples of this on the web use HQL instead of the ICriteria API and since we primarily use the ICriteria API we looked into how to do this.

Turns out it is pretty simple to do, though the syntax isn't really straightforward. Suppose you want to query all of your employees who are born in a specific year. You could mess around with some DateTime parameters, but most databases have SQL functions to get the year from a date. Using the ICriteria API, this would look like this:

            var employeesBornIn81 = session.CreateCriteria<Employee>()
                .Add(Restrictions.Eq(Projections.SqlFunction("year", NHibernateUtil.DateTime, Projections.Property("BirthDate")), 1981))
                .List<Employee>();

which adds the following where clause to the SQL statement (on SQL Server 2005):

WHERE datepart(year, this_.BirthDate) = @p0;

  • Koen

    1981 was a good year…

  • http://davybrion.com Davy Brion

    i used to think so ;)

  • http://benpittoors.wordpress.com den Ben

    get off my lawn you 2!!

  • Pingback: Reflective Perspective - Chris Alcock » The Morning Brew #339

  • marek

    Hi Davy,

    what if the db doesn’t support the ‘year’ function?
    Doesn’t your code become db-specific when you use this kind of syntax?

    Thanks

  • http://davybrion.com Davy Brion

    it’s not entirely db-specific, because these functions are registered per dialect and then map to their DB-specific implementation

    if you use a function that’s only supported by one (or a couple of) dialect, then yes, you lose some db portability.

    you can find the supported functions, and the db-specific implementation they map to, in the constructor of each dialect

  • pho

    what about functions that need additional parameters, such as DATEPART or SUBSTRING? are those possible?

  • http://davybrion.com Davy Brion

    there are some functions that are registered per dialect that support multiple parameters though the way to use them with the Criteria API is probably a bit different… haven’t tried those yet :)

  • Rizwan Akram

    how to use function’s parameters like TO_CHAR(date_column, ‘dd-mon-yyyy’), how I supply ‘dd-mon-yyyy’ as parameter in the example?