Search code examples
javasqlhibernatehql

Sort by date ascending with nulls last


I use java and hibernate 3.2.5.ga .

I have a table with a date column. I want to create a hql query that will sort the result by date ascending but will put the nulls last.

Normally I the sort by date asc returns nulls first and the "NULL LAST" keyword does not work.

How can I achieve that ?


Solution

  • Use query like this:

    SELECT YourDateColumn
    FROM YourTable
    WHERE YourCondition
    ORDER BY
      CASE WHEN YourDateColumn IS NULL THEN 1 ELSE 0 END,
      YourDateColumn