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, p2Firstetc
I need to get:
p1Last, p1First
p2Last, p2First null, null null, nullAny 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
精彩评论