Jump to content

Archived

This topic is now archived and is closed to further replies.

Kris

Select a field as a different datatype?

Recommended Posts

I have a field set as a varchar datatype, but on occasion I would need to treat it as if it were a float field in a php script, sometimes it may have a price in there that I wish to order from ascending to descending, and ofcourse, it doesn't work as expected when its a varchar field.
I have tried something like this, with no luck, can this even be done?
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] `price`[color=green]AS[/color] FLOAT() [color=green]FROM[/color] [color=orange]`table`[/color] [color=green]ORDER BY[/color] `price` [color=green]ASC[/color] [!--sql2--][/div][!--sql3--]

Share this post


Link to post
Share on other sites
[code]SELECT CAST(`price`AS DECIMAL) AS myprice FROM table ORDER BY myprice ASC[/code]

This will NOT use any index you may have on `price`, so it will have to sort it every time you make the query. The best way to handle this is to change the column in mysql to a DECIMAL or FLOAT type.

Share this post


Link to post
Share on other sites
Ok, I'll look into that. Thank you for your advice.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.