How to order by column with non-null values first in sql

后端 未结 3 1194
名媛妹妹
名媛妹妹 2020-12-31 03:48

I need to write a sql statement to select all users ordered by lastname, firstname. This is the part I know how to do :) What I don\'t know how to do is to order by non-nu

相关标签:
3条回答
  • 2020-12-31 04:06

    See Sort Values Ascending But NULLS Last

    basically

    SELECT *
        FROM @Temp
        ORDER BY CASE WHEN LastName IS NULL THEN 1 ELSE 0 END, LastName
    
    0 讨论(0)
  • 2020-12-31 04:08
    ORDER BY CASE WHEN name IS NULL THEN 1 ELSE 0 END, name;
    
    0 讨论(0)
  • 2020-12-31 04:15

    Nowadays the
    IIF ( boolean_expression, true_value, false_value )

    will suit too.

    ORDER BY IIF(name IS NULL, 1, 0), name

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