ORDER BY DATE showing NULLS first then most recent dates

前端 未结 8 1354
无人共我
无人共我 2020-11-29 07:21

I have a stored procedure which executes a select statement. I would like my results ordered by a date field and display all records with NULL dates first and then the most

8条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 07:45

    @Chris, you almost have it.

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

    [Edit: #Eppz asked me to tweak the code above as currently shown]

    I personally prefer this a lot better than creating "magic numbers". Magic numbers are almost always a problem waiting to happen.

提交回复
热议问题