Jump to content

Select a field as a different datatype?


zq29

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--]
Link to comment
https://forums.phpfreaks.com/topic/5335-select-a-field-as-a-different-datatype/
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.

Archived

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

×
×
  • Create New...

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.