3

I've got a database in postgres and in few columns (type int) I've got NaN values. When I'm sorting ASC the result is correct, for example:

0
1
2
3
4
NaN

But when I'm sorting DESC I've got:

naN
4
3
2
1
0

I know that Postgres treats NaN values as equal, and greater than all non-NaN values, but Is there a way to get this result?

4
3
2
1
0
NaN

Any ideas?

1 Answer 1

5

If your column doesn't contain NULLs, you can unambiguously convert NaNs to NULLs and sort on that:

select *
from some_table
order by nullif(some_column, 'NaN') desc nulls last
Sign up to request clarification or add additional context in comments.

2 Comments

NULLS LAST did the trick :) But does it work for text values too?
@DanielKoczuła How do you get NaN in a text column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.