order by
case
when column is null then 1 else 0
end,
column
OR
ORDER BY ISNULL(column), column ASC;
OR
ORDER BY if(column = '' or column is null,1,0),column
Question
I have a problem ordering a table.
I have a column of strings. These strings can be numeric (integer).
Now I want to order by ascending, leaving numbers on top and strings at the bottom, with the empty ones last:
12
23
28
33
Hello
Hello again
(empty string)
(empty string)
This is the problem, if I go with:
ORDER BY column ASC
this happens:
(empty string)
(empty string)
12
23
28
33
Hello
Hello again
I tried with:
ORDER BY CAST(column as unsigned) ASC, column ASC/DESC
but it puts number values at the bottom:
Hello again
Hello
(empty string)
(empty string)
(empty string)
12
23
28
33
How may I solve that? Thank you very much!
Solution
order by
case
when column is null then 1 else 0
end,
column
OR
ORDER BY ISNULL(column), column ASC;
OR
ORDER BY if(column = '' or column is null,1,0),column