60

I have tasks in a MySQL database, and one of the fields is deadline date. Not every task has to have to a deadline date.

How do I use SQL to sort the tasks by deadline date, but put the ones without a deadline date in the back of the result set.

Now, the null dates show up first, then the rest are sorted by deadline date earliest to latest.

4 Answers 4

81

Here's a solution using only standard SQL, not ISNULL(). That function is not standard SQL, and may not work on other brands of RDBMS.

SELECT * FROM myTable
WHERE ...
ORDER BY CASE WHEN myDate IS NULL THEN 1 ELSE 0 END, myDate;
7
  • +1. Here's another elegant and compact standard solution (available since SQL:2003 extension T611, although not all vendors implement this functionality): stackoverflow.com/a/12767777/814702. Commented Jan 31, 2014 at 14:50
  • 1
    @informatik01, good tip, but this question is tagged mysql and MySQL doesn't support that syntax. Commented Jan 31, 2014 at 23:07
  • @BillKarwin Yeah, sad. Only checked this on PostgreSQL 9.1, where it works just fine. Anyways, let this info be here just for the sake of completeness. P.S. Just noticed that when using ORDER BY MySQL and PostgreSQL behave differently: when ordering ascending MySQL puts NULL values first, while PostgreSQL puts NULL values last. Well, vendor specific stuff ... Commented Feb 1, 2014 at 12:01
  • To explain how this works - stackoverflow.com/questions/26985068/… Commented Nov 18, 2014 at 1:37
  • 1
    @MasterJoe, you can order by any column you want. But in MySQL, NULL is ordered before any other value. You could use order by ... desc as you say, but then all the values would be sorted in reverse order. I assume the question wants them sorted ascending, except for NULLs which should be after all other values. Commented Sep 1, 2020 at 21:27
30
SELECT * FROM myTable
WHERE ...
ORDER BY ISNULL(myDate), myDate
3
  • ISNULL requires 2 arguments on SQL Server. Does MySQL only require one?
    – Codewerks
    Commented Sep 29, 2008 at 23:48
  • 3
    yep - only one. dev.mysql.com/doc/refman/5.0/en/…
    – nickf
    Commented Sep 29, 2008 at 23:50
  • Note that, in the case of Access/VBA, True < False, so you may need ORDER BY Not IsNull (field), field.
    – Martin F
    Commented Apr 14, 2015 at 23:24
6

The easiest way is using the minus operator with DESC.

SELECT * FROM request ORDER BY -date DESC

In MySQL, NULL values are considered lower in order than any non-NULL value, so sorting in ascending (ASC) order NULLs are listed first, and if descending (DESC) they are listed last.

When a - (minus) sign is added before the column name, NULL become -NULL.

Since -NULL == NULL, adding DESC make all the rows sort by date in ascending order followed by NULLs at last.

4
SELECT foo, bar, due_date FROM tablename
ORDER BY CASE ISNULL(due_date, 0)
WHEN 0 THEN 1 ELSE 0 END, due_date

So you have 2 order by clauses. The first puts all non-nulls in front, then sorts by due date after that

2
  • "case isnull(due_date, 0) when 0 then 1 else 0" - why bother doing all that? isnull returns 1 or 0 anyway.
    – nickf
    Commented Sep 29, 2008 at 23:41
  • In MSSQL, ISNULL returns its first argument, or its second argument if its first argument is null. There's a standard SQL function COALESCE which does the same and also accepts more than two arguments. But this is a bit off-topic since the poster is asking about MySQL....
    – Stewart
    Commented Dec 16, 2014 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.