ORDER BY DATE showing NULLS first then most recent dates

Sql ServerSql Server-2000

Sql Server Problem Overview


I have a stored procedure which executes a select statement. I would like my results ordered by a date field and display all records with NULL dates first and then the most recent dates.

The statement looks like this:

SELECT a,b,c,[Submission Date]
FROM someView
ORDER BY [Submission Date] ASC

Now this will display all records with NULL Submission Dates first, but when I get to rows that have date values in them, they are not the most recent dates in the view.

If I replace ASC with DESC, then I get the dates in the the order I want, but the NULL values are at the bottom of my result set.

Is there any way to structure my query so that I can display the null values at the top and then when there are date values, to order them descending most recent to oldest?

Sql Server Solutions


Solution 1 - Sql Server

@Chris, you almost have it.

ORDER BY (CASE WHEN [Submission Date] IS NULL THEN 1 ELSE 0 END) DESC, 
         [Submission Date] DESC

[Edit: #Eppz asked me to tweak the code above as currently shown]

I personally prefer this a lot better than creating "magic numbers". Magic numbers are almost always a problem waiting to happen.

Solution 2 - Sql Server

You can do something like this put the NULL's at the bottom:

ORDER BY [Submission Date] IS NULL DESC, [Submission Date] ASC

Solution 3 - Sql Server

Standard SQL (ISO/IEC 9075-2:2003 or later - 2008) provides for:

ORDER BY SomeColumn NULLS FIRST

Most DBMS do not actually support this yet, AFAIK.

Solution 4 - Sql Server

try

SELECT a,b,c,[Submission Date]
FROM someView
ORDER BY isnull([Submission Date],cast('2079/01/01' as datetime)) ASC

Solution 5 - Sql Server

OrderBy="ColumnName = NULL desc, ColumnName desc"

Solution 6 - Sql Server

I have another suggestion that might be simpler than all the others:

For SQL Server, most of the options don't work, except the case ones.

I found that this actually works great to me: ORDER BY ISNULL(Submission_Date, GETDATE()) DESC

In the order bit of the query, I assign the GETDATE() value to the Submittion_Date values that are null, and the order comes out correctly.

Solution 7 - Sql Server

try this

> SELECT a,b,c,[Submission Date] > FROM > someView > ORDER BY > isnull([Submission Date] ,cast('1770/01/01' as datetime)) ASC

Solution 8 - Sql Server

I know this is old, but when I found it I noticed the accepted solution, https://stackoverflow.com/a/821856/7177892, could be simplified by making the result of the CASE statement be either today (GETDATE()) or the actual date.

Original:

ORDER BY (CASE WHEN [Submission Date] IS NULL THEN 1 ELSE 0 END) DESC, 
         [Submission Date] DESC

Simplified:

ORDER BY (CASE WHEN [Submission Date] IS NULL 
               THEN GETDATE() 
               ELSE [Submission Date] 
          END) DESC

Solution 9 - Sql Server

Assuming Submission Date less than current date-time

SELECT a,b,c,[Submission Date]
FROM someView
ORDER BY ISNULL([Submission Date], GETDATE()) DESC

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEppzView Question on Stackoverflow
Solution 1 - Sql ServerEuro MicelliView Answer on Stackoverflow
Solution 2 - Sql ServerChris BartowView Answer on Stackoverflow
Solution 3 - Sql ServerJonathan LefflerView Answer on Stackoverflow
Solution 4 - Sql ServerSadeghView Answer on Stackoverflow
Solution 5 - Sql ServerPaulView Answer on Stackoverflow
Solution 6 - Sql ServerMarco LeiteView Answer on Stackoverflow
Solution 7 - Sql ServerlittlechrisView Answer on Stackoverflow
Solution 8 - Sql ServernconantjView Answer on Stackoverflow
Solution 9 - Sql ServerhanView Answer on Stackoverflow