How can i convert a price field which is currently varchar to a decimal so my prices will order correctly?

StackOverflow https://stackoverflow.com/questions/2081169

  •  21-09-2019
  •  | 
  •  

문제

I am using MYSQL.

I have a varchar field which i incorrectly used for a price. Now the ordering of this table will not work correctly putting anything over 1000 to the bottom of the list.

I need to convert this price field in an existing POPULATED database from varchar to decimal i guess?

Any help would be appreciated.

도움이 되었습니까?

해결책

Simply use the ALTER TABLE statement.

If for example the table is called 'products' and the field is called 'product_price' you could simply use:

ALTER TABLE products MODIFY COLUMN product_price DOUBLE;

NB: As with anything, I'd be very tempted to make a backup of the data (via mysqldump) prior to performing this operation - it'll take seconds and it's always better to be safe rather than sorry. :-)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top