ORDER BY with NULL values

I have a column in a MySQL database which can contain either 1,2,3 or be NULL. I need to extract the data, sorting it in such a way that 1,2,3 come first, the NULL rows after.
By default, MySQL puts the NULL values first, unless you specify DESC but then the results are in the reverse order! Any advice much appreciated!
bergsyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tbcoxCommented:
The better solution is to order by NVL(my_col,4), if NVL is supported by your version of MySQL.  NVL takes an input column and passes back, unchanged, all non-NULL values in the column; NVL will trap NULLs and pass back whatever value is in the second argument, in this case, '4'.

So if your table contained 1,2,3,NULL as values, the above NVL would return 1,2,3,4.

The much less nice solution involves updating your data to change NULL to 4, or creating an ORDER BY column containing this conversion, etc. -- very ugly stuff.

See if NVL works for you.
0
DedushkaCommented:
Hi bergsy.

Just sort by negative values ...

SELECT tblMyTable.fldID, tblMyTable.fldSomeTextField
FROM tblMyTable
ORDER BY -[fldID] DESC;

Cheers,
Dedushka
0
bergsyAuthor Commented:
Nice idea tbcox, but as far as I can find out MySQL doesn't have the NVL function. But if you know otherwise, please tell me!

I've used a variant of Dedushka's answer, which wasn't quite correct but pointed me in the right direction :-

SELECT rank, (0-rank) AS foo
FROM mytable
ORDER BY foo DESC;

The syntax is less elegant than Dedushka's, but MySQL seems fussy on this point!
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

LischkeCommented:
Hi guys,

what about using a UNION where the first select clause just selects and orders non-null values and the second select clause just returns rows with null values?

Although, Dedushka's idea is not bad...not bad :-)

Ciao, Mike
0
DedushkaCommented:
So I win this points? :-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gustavonevesCommented:
Since tbcox's comment is not suitable (that would be my first choice, too) see if this works. The idea is to use an additional table just to help you with the sort problem. Let's call this table tblAdTable and build it with 2 fields: "contents" and "order"

1 - Load it like this:
     contents   order
     ------------------
     1                1
     2                2
     3                3
     NULL           4

2 - Now join field "contents" from tblAdTable with the column in your table, and sort the query by tblAdTable.order;

HTH

 






0
bergsyAuthor Commented:
Dedushka's solution seems the most elegant, and it's what I have adopted.
Building another table is impractical as a) It's inefficient and b) The actual column was generated on the fly using a natural left join, & I wanted to keep the whole thing as one simple statement.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.