SQL how to make null values come last when sorting ascending
(A "bit" late, but this hasn't been mentioned at all)
You didn't specify your DBMS.
In standard SQL (and most modern DBMS like Oracle, PostgreSQL, DB2, Firebird, Apache Derby, HSQLDB and H2) you can specify NULLS LAST
or NULLS FIRST
:
Use NULLS LAST
to sort them to the end:
select *
from some_table
order by some_column DESC NULLS LAST
select MyDate
from MyTable
order by case when MyDate is null then 1 else 0 end, MyDate