Reputation: 65
I have a MySQL table named "Amount", which has amounts like 200, 300, 100 etc. But, what I want is, that the amounts should be 200.00, 300.00,100.00. How do I write a query to update the column, so that it adds the ending zeroes? The column is float.
I tried with a regular update query, but it failed and I had to revert the changes (re-upload a backup sql file).
Any suggestions are welcome.
Upvotes: 0
Views: 1029
Reputation: 400
I think your best solution is an update your column
something like
ALTER TABLE Amount
MODIFY amount
float(7,2) NOT NULL DEFAULT 0;
Upvotes: 2
Reputation: 538
update Amount set floati = concat(floati,'.00');
Example: http://sqlfiddle.com/#!2/fa240/1
Upvotes: 0
Reputation: 53781
Number columns in MySQL (and probably most db engines) don't actually save numbers like that, but binary. If you're interested, it's probably something like IEEE.
There is a data type that does save numbers literally: DECIMAL
. If you save 5.2
into a DECIMAL(2, 4)
, it saves the number as 5.2000
. Decimals are saved as strings and only 'converted' into a number when it's saved into the db.
You could try altering the column into a DECIMAL(n, 2)
, but depending on how smart MySQL is, that doesn't update the values correctly:
ALTER TABLE your_table
MODIFY your_column DECIMAL(5, 2) NOT NULL DEFAULT 0 -- etc
Upvotes: 0