开发者

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

I need to write a sql statement to select all users ordered by lastname, firstname. This is the part I know how to do :) W开发者_如何学JAVAhat I don't know how to do is to order by non-null values first. Right now I get this:

null, null

null, null

p1Last, p1First

p2Last, p2First

etc

I need to get:

p1Last, p1First

p2Last, p2First

null, null

null, null

Any thoughts?


See Sort Values Ascending But NULLS Last

basically

SELECT *
    FROM @Temp
    ORDER BY CASE WHEN LastName IS NULL THEN 1 ELSE 0 END, LastName


ORDER BY CASE WHEN name IS NULL THEN 1 ELSE 0 END, name;


Nowadays the
IIF ( boolean_expression, true_value, false_value )

will suit too.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜