SQL Server equivalent to Oracle's NULLS FIRST?

前端 未结 8 586
心在旅途
心在旅途 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:08

    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
    

提交回复
热议问题