Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Beginner
Jun 16, 2016

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 MyDateColumn
FROM MyTable
ORDER 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

Hannes du Preez
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date