views:

122

answers:

1

My table has three boolean fields: f1, f2, f3. If I do

SELECT * FROM table ORDER BY f1, f2, f3

the records will be sorted by these fields in the order false, true, null. I wish to order them with null in between true and false: the correct order should be true, null, false.

I am using PostgreSQL.

+5  A: 

Not beautiful, but should work:

... order by (case when f1 then 1 when f1 is null then 2 else 3 end) asc

meriton
Obviously you would have to do the same for each of the other variables.
Jeremy Roberts
+1. Let's make it uglier: `ORDER BY COALESCE((NOT bool_field)::INTEGER * 2, 1)`. :)
pilcrow
ERROR: Function isnull(boolean) does not exist
petehern
should work now
meriton
Replaced isnull(f1) with f1 IS NULL, works. Thanks!
petehern