# MYSQL - Dynamic Column Type Change

**URL:** <https://forums.percona.com/t/mysql-dynamic-column-type-change/899>\
**Category:** Other MySQL® Questions\
**Created:** [August 27, 2008, 4:34am UTC](https://forums.percona.com/t/mysql-dynamic-column-type-change/899 "2008-08-27T04:34:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hk\_178](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@hk\_178](https://forums.percona.com/u/hk_178)\
**Post date:** [August 27, 2008, 4:34am UTC](https://forums.percona.com/t/mysql-dynamic-column-type-change/899/1 "2008-08-27T04:34:48Z")

</div>

Hi,  
I am trying to run one query in my Mysql Database to calculate Gross Profit nd Margins from few tables.

I’m selecting final value of gross profit in query like :

format(((prod.selling\_price + ship.rate) - (prod.cost\_price + ship.shipping\_cost + sup.fee\_per\_order + sup.extra\_fee\_per\_product + sup.other)),2) as gross\_profit.

Now I want to sort results on this gross\_profit ASC / DESC depends on requirement - when im doing sorting on this gross\_profit its not giving accurate result coz its taking values of gross\_profit as string.

Is ny way i can convert this gross\_profit to INT so my sorting give accurate results or any other way to get accurate result in sorting?

I tried to use like ORDER BY CAST(gross\_profit AS SIGNED) but not work as i understand its not column from table it self.

Can any one help me to sort out this?..

Thanks in Advance !

---

<div class="post-metadata">

**Author:** ![varmadba](https://avatars.discourse-cdn.com/v4/letter/v/ba8739/32.png) [@varmadba](https://forums.percona.com/u/varmadba)\
**Post date:** [August 27, 2008, 8:45am UTC](https://forums.percona.com/t/mysql-dynamic-column-type-change/899/2 "2008-08-27T08:45:23Z")

</div>

Try

CAST(gross\_profit AS decimal(65,4))

---

<div class="post-metadata">

**Author:** ![hk\_178](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@hk\_178](https://forums.percona.com/u/hk_178)\
**Post date:** [August 27, 2008, 9:33am UTC](https://forums.percona.com/t/mysql-dynamic-column-type-change/899/3 "2008-08-27T09:33:08Z")

</div>

Hi,

Thanks for reply )

This is what im gettig now…

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘decimal ( 65 , 4 ) ) DESC  
LIMIT 0, 30’ at line 1

so i think ORDER BYCAST(  
gross\_profit AS decimal( 65, 4 )  
) will not work as well…

Thanks !!!
