views:

729

answers:

6

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?

+1  A: 

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.

David Andres
The problem with this is as GMastros said...
Kev
Kev: It all depends on the nature of the problem at hand. The question was not "is this the ORDER BY clause to use in all cases?," and so G Mastros point is well-noted but not strictly on the critical path of the question itself. rexem is looking for a more global way of controlling NULL sorting.
David Andres
+2  A: 

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
Novitzky
+8  A: 

You can do some trick:

ORDER BY (CASE WHEN [Order] IS NULL THEN 0 ELSE 1 END), [Order]
Lukasz Lysik
With this trick you can achieve any ordering you want. You can even mimic the more general IComparer interface found in many languages.
usr
+2  A: 

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.

G Mastros
Order by IsNull(Date_Sent, '17530101') desc would ensure my columns with a null date_sent value appear at the bottom, not the top. You are correct, but I don't travel into the future to set the date_sent value =)
OMG Ponies
Good point, but the principle still holds. You should use a value that will not naturally occur in your data. Ex: Order by IsNull(Date_Sent, '99990101') desc
G Mastros
Totally agree on use of a sentinel value.
OMG Ponies
+1  A: 
ORDER BY
  COALESCE(POSTING_DATE,'1900-01-01 00:00:00.000')
 ,OTHER_FIELDS
JosephStyons
Doesn't work - nulls are first, but the rest of the date_sent columns aren't sorted in DESC order.
OMG Ponies
+1  A: 

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
PaulG
Confirmed: Nulls are at the top, and non-null values are in DESC order.
OMG Ponies