Putting a NULL Date Value Last

Sometimes when presenting your query’s value, you end up with NULL values, obviously. The irritating thing about NULL values, especially with NULL Date values, is that when you attempt to sort a Date column containing NULL values, the NULL values always display first — even after a sort — it will always occupy the first row.

In order to circumvent this behavior, you need to tweak your ORDER BY clause a little. Here is a small sample:

SELECT MyDateColumnFROM MyTableORDER BY CASE WHEN MyDateColumn IS NULL THEN 1 ELSE 0 END, MyDateColumn

This forces a ‘1’ value into the column when a NULL value is present, otherwise ‘0’. This ends up placing the NULL Value(s) at the bottom

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Related Posts