Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ordering a food menu by 2 columns: dish & dishprice MSSQL

I’m trying to order by 2 columns: dish and dish price. They are both stored as strings so it casts the dishprice to Decimal. Its basically a takeaway food menu, so I want it to start with the lowest price item and end with the most expensive item. However, it’s nice to have them in some sort of alphabetic order too. Here’s what I’m getting for the sundries menu:

MS SQL:

SELECT dish, dishPrice, category FROM tbldishes ORDER BY CAST(dishPrice AS DECIMAL) ASC, dish ASC

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Barbeque Sauce 0.85
Coke Zero 1.00
Coleslaw 0.85
Garlic Mayo 0.85
Pepsi Max 1.00
Chips 2.95

It want and would have expected this..

Barbeque Sauce 0.85
Coleslaw 0.85
Garlic Mayo 0.85
Coke Zero 1.00
Pepsi Max 1.00
Chips 2.95

Which keeps the sauces together and drinks together whilst still in price order. The menu’s are obviously much bigger than this. Any help appreciated.

>Solution :

If you change your query to

SELECT dish, CAST(dishprice), category FROM tbldishes

you will note that the second column is an integer in all cases (0,1,2)
try using

SELECT dish, dishPrice, category FROM tbldishes ORDER BY CAST(dishPrice AS DECIMAL(18,2)) , dish 

Or better yet, use the correct data type in your table.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading