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
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.