Order by Numeric then Alpha

SELECT * FROM locations ORDER BY cast(X as decimal), X, Y

This code prints like this:
ADMIN
CAFE
CDC
CMAT
1
1.5
2
10
10.5
18

I am looking for a way to put the alpha on the bottom and the numeric on top in the same order as shown.

Are the values you showed as output in different columns of the database? Usually you would ORDER BY column name ASC or DESC depending on what you want and then use a comma for the next order by and again you can specify the ASC or DESC but it will default to ASC if not given.

Also, why are you casting X to decimal? It would be better if you are storing it that way in the first place, unless you have some specific reason not to, but I couldn’t fathom a reason that you wouldn’t.

Sponsor our Newsletter | Privacy Policy | Terms of Service