Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Something to get started with.</p> <p>Select Birthdays where Birthday between now-5 days and now+10 days.</p> <p><strike></p> <pre><code> var today = DateTime.Now(); var results = session.CreateQuery("select p.Birthday from Person where p.Birthday&gt;= :from and p.Birthday&lt;=:to") .SetParameter("from", today.AddDays(10)) .SetParameter("to", today.Subtract(new TimeSpan(5, 0, 0, 0))) .List&lt;DateTime&gt;(); </code></pre> <p></strike> Although I think you want to get birthdays regardless of the year.</p> <p>In order to replicate this query in HQL you are going to have to register the specific SQL functions you need using this technique:</p> <p><a href="http://nhforge.org/blogs/nhibernate/archive/2009/03/13/registering-freetext-or-contains-functions-into-a-nhibernate-dialect.aspx" rel="nofollow noreferrer">Register Custom SQL Function in NHibernate</a></p> <p>Then you should be able to replicate your SQL query in HQL.</p> <p>This is a good question on the SQL side of things:</p> <p><a href="https://stackoverflow.com/questions/83531/sql-select-upcoming-birthdays">SQL Select Upcoming Birthdays</a></p> <hr> <p>Fresh tactics:</p> <p>Register the SQL Function for the datediff:</p> <pre><code>RegisterFunction("datediffdays", new SQLFunctionTemplate(NHibernateUtil.Int32, "datediff(dd,?1, ?2)")); </code></pre> <p>HQL Query</p> <pre><code>var result = session.CreateQuery(@"from Person where 1 = (floor(datediffdays(Birthday,current_timestamp()+10) / 365.25)) - (datediffdays(Birthday,current_timestamp()-5) / 365.25))") .List&lt;Person&gt;(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload