ORDER BY DATE showing NULLS first then most recent dates

前端 未结 8 1233
无人共我
无人共我 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:50

    Standard SQL (ISO/IEC 9075-2:2003 or later - 2008) provides for:

    ORDER BY SomeColumn NULLS FIRST
    

    Most DBMS do not actually support this yet, AFAIK.

    0 讨论(0)
  • 2020-11-29 07:51

    I have another suggestion that might be simpler than all the others:

    For SQL Server, most of the options don't work, except the case ones.

    I found that this actually works great to me: ORDER BY ISNULL(Submission_Date, GETDATE()) DESC

    In the order bit of the query, I assign the GETDATE() value to the Submittion_Date values that are null, and the order comes out correctly.

    0 讨论(0)
提交回复
热议问题