SQL Server equivalent to Oracle's NULLS FIRST?

SqlSql ServerSql Server-2005Tsql

Sql Problem Overview


So Oracle has NULLS FIRST, which I can use to have null values sorted at the top followed by my column value in descending order:

ORDER BY date_sent NULLS FIRST

What is comparable in SQL Server? There are these alternatives, assuming the date values are NULL or in the past:

ORDER BY ISNULL(date_sent, GETDATE()) DESC
ORDER BY (CASE WHEN t.setinactive IS NULL THEN 1 ELSE 2 END), t.setinactive DESC
ORDER BY -CAST(date_sent as int) ASC

Any others?

Sql Solutions


Solution 1 - Sql

You can do some trick:

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

Solution 2 - Sql

The quick answer is this: the best solution for changing the ordering of nulls in the necessary cases is the accepted one. But you only have to use it, or a variation of it in the necessary cases:

  • DESC + NULLS FIRST:

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

  • ASC + NULLS LAST:

    ORDER BY (CASE WHEN [Order] IS NULL THEN 1 ELSE 0 END), [Order] ASC

  • ASC + NULLS FIRST: it works fine by default

  • DESC + NULLS LAST: it works fine by default

Let's see why:

If you check the ORDER BY Clause (Transact-SQL) MSDN docs, and scroll down to ASC | DESC, you can read this:

> ASC | DESC > Specifies that the values in the specified column should be sorted in ascending or descending order. ASC sorts from the lowest value to highest value. DESC sorts from highest value to lowest value. ASC is the default sort order. Null values are treated as the lowest possible values.

So, by default if you specify ASC order, it works like NULLS FIRST. And, if you specify DESC, it works like NULLS LAST.

So you only need to do change the behavior for NULLS FIRST in DESC order, and for NULLS LAST in ASC order.

IMHO, the best solution for changing the ordering of nulls in the necessary cases is the accepted one, but I've included it adapted to the different cases in the beginning of my answer.

Solution 3 - Sql

Use Case/When statement, for example:

ORDER BY (case WHEN ColINT IS NULL THEN {maxIntValue} ELSE ColINT END) DESC

ORDER BY (case WHEN ColVChar IS NULL THEN {maxVCharValue} ELSE ColVChar END) DESC

ORDER BY (case WHEN ColDateT IS NULL THEN {maxDateTValue} ELSE ColDateT END) DESC

...and so on.

or even better as you don't care what is your column type and the max value.

ORDER BY (case WHEN ColAnyType IS NULL THEN 1 ELSE 0 END) DESC, ColAnyType DESC

Solution 4 - Sql

If you have rows in your table with dates less than now, and other rows with dates greater than now, your NULLS would appear in the middle of the list. Instead, you should probably use a value that will never sort in the middle of your list.

Order by IsNull(Date_Sent, '17530101') desc

Note: That date is actually Jan 1, 1753.

Solution 5 - Sql

A simple example:

SELECT (CASE WHEN Value1 IS NULL THEN 1 ELSE 0 END) AS ValueIsNull, Value1, Value2, Value3
FROM TableName
ORDER BY ValueIsNull DESC, Value1 

Solution 6 - Sql

ORDER BY
  COALESCE(POSTING_DATE,'1900-01-01 00:00:00.000')
 ,OTHER_FIELDS

Solution 7 - Sql

This is an alternative way when you want to adjust how nulls appear in the sort order. Negate the column and reverse your sort order. Unfortunately you would need to CAST dateTime columns.

ORDER BY -CAST(date_sent as int) ASC

Solution 8 - Sql

You can't control this, to my knowledge. And it looks like you have the correct approach with ISNULL.

With strings, I've used ISNULL(field, '') for the same purpose.

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
QuestionOMG PoniesView Question on Stackoverflow
Solution 1 - SqlLukasz LysikView Answer on Stackoverflow
Solution 2 - SqlJotaBeView Answer on Stackoverflow
Solution 3 - SqlNovitzkyView Answer on Stackoverflow
Solution 4 - SqlGeorge MastrosView Answer on Stackoverflow
Solution 5 - SqlJonatas SellosView Answer on Stackoverflow
Solution 6 - SqlJosephStyonsView Answer on Stackoverflow
Solution 7 - SqlPaulGView Answer on Stackoverflow
Solution 8 - SqlDavid AndresView Answer on Stackoverflow