devxlogo

Putting a NULL Date Value Last

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

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist