SQL Order By with Nulls at the end.
April 27th, 2010 by KarlI wanted to order a column alphabetically however NULLS show up first. I want the items will null, but i want them after the rest so i applied this like quick and dirty order by clause.
order by isnull(displayname,’zzzzzzz’)
it effectively puts nulls at the end. If you know a cleaner and just as pithy way I’d love to hear it.
Posted in SQL | 3 Comments »

April 28th, 2010 at 12:09 am
select…
where … is not null
order by…
union all
select …
where … is null
April 28th, 2010 at 12:48 am
order by
case when displayname is null then 1 else 0 end,
displayname
– still works if displayname happens to begin with many Zs
May 6th, 2010 at 9:22 am
Here’s what I’m using in a PostgreSQL-backed app:
ORDER BY CASE WHEN whatever_column IS NULL THEN 1 ELSE 0 END, whatever_column
Cleaner in the sense that if you have something called ‘Zzzzzzzzzzz’, it would sort properly.
Not quite as pithy though.