I use SQL Server 2008 R2.
I need to sort a table by the minimal value of two columns.
The table looks like this:
ID: integer;
Date
I would order the rows by this rules:
To do this a nested case will be simple and efficient (unless the table is very large) according to this post.
SELECT ID, Date1, Date2
FROM YourTable
ORDER BY
CASE
WHEN Date1 IS NULL AND Date2 IS NULL THEN 0
WHEN Date1 IS NULL THEN 1
WHEN Date2 IS NULL THEN 2
ELSE 3 END,
CASE
WHEN Date1 < Date2 THEN Date1
ELSE Date2
END