SQL Server equivalent to Oracle's NULLS FIRST?

前端 未结 8 577
心在旅途
心在旅途 2020-12-08 00:50

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 FI         


        
相关标签:
8条回答
  • 2020-12-08 01:17

    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.

    0 讨论(0)
  • 2020-12-08 01:21

    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 
    
    0 讨论(0)
提交回复
热议问题