Forum Moderators: open
However, when I do the opposite: ORDER BY PRICE DESC, the entries with the NULL price value are displayed first, followed by the highest price items working downward to the lowest price.
What would be the proper way or SQL syntax to ALWAYS display items with NULL prices at the end?
Instead of NULL for items without prices should I instead use some other value ($0.00 doesn’t work either)?
The column type is MONEY.
SELECT price,
CASE WHEN price is null THEN 0.00
END
FROM table
order by price desc;
a Coalesce would look like this.
SELECT
COALESCE(price, 0.00) as price
from table
order by price desc
Coalesce is meant to substitute null values with a default value. When using it though it must use the same datatype as is used by the table itself.
So you can't go:
SELECT
COALESCE(price, 'Price Not Available') as price
from table
order by price desc
Because the value 'Price Not Available' is a string not a double or whatever datatype you are using.
As I mentioned, ORDER BY PRICE ASC works correct for sorting from lowest-to-highest with NULL values at the end.
The following clause "ORDER BY (price IS NULL), price DESC" works for sorting highest-to-lowest with NULL values at the end.
Thanks to everyone who replied!