简体   繁体   中英

SQL Server equivalent to Oracle's NULLS FIRST?

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?

你可以做一些技巧:

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

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 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.

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

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.

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 
ORDER BY
  COALESCE(POSTING_DATE,'1900-01-01 00:00:00.000')
 ,OTHER_FIELDS

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM