I have a database with a column named "price" - within this column there are prices rounded to 5 decimal places, e.g 62.083333 - I would like to run a command to change every value within "price" column to just 2 decimal places, e.g 62.08
Is there a simple command to run on this column? We have over 36,000,000 values so would prefer not to have to update them all manually! :)
Would this command be the one?
SELECT CONVERT(DECIMAL(20,2),price)
Here's a similar question: mass update mysql table decimal value and change column field type
You can use
ALTER TABLE '<name>' CHANGE COLUMN '<name>' DECIMAL(20,2)
Simply alter table and modify data type
ALTER TABLE [TableName] MODIFY COLUMN [ColumnNameHere] decimal(20,2)
Try ,
UPDATE table_name SET price = round(price,2) where your
conditions;
Related
I have a column of decimals, but MySQL is reading them as TEXT. I tried this to alter the column by:
ALTER TABLE `engine_type_project`.`weo_data_eu_test`
CHANGE COLUMN `Mean_GDP_all_time` `Mean_GDP_all_time` DECIMAL(6,2) NULL DEFAULT NULL;
An original value is: 3,282.772
But my code returns it as: 3.00
Prior to this, I attempted:
SELECT CAST('Mean_GDP_all_time' AS DECIMAL(6,2))
FROM weo_data_eu_test;
But the entire column returned as 0.00
In casting non-numeric values to numeric, mysql does not expect commas. So it gives up looking for additional parts of the number after the "3".
Before changing the type, remove the commas with:
update weo_data_eu_test set Mean_GDP_all_time=replace(Mean_GDP_all_time,',','')
fiddle
i have one field which contain data like 4563******3245. when i execute my sql query it is inserted successfully. but in mysql database it is showing only 4563
my sql query is:
insert into mytable ('myfield') values ('4563******3245');
Can any one tell me where is the problem.
thank you.
insert into mytable ('myfield') values ('4563******3245');
it is working fine
make sure your column type should be varchar
You can't store text in a column of a number data type. You have to change your data type to char(14).
ALTER TABLE your_table MODIFY myfield CHAR(14);
If your running that particular query, and only 4563 is being inserted, it would suggest your column type for "myfield" is set as some variant or length of "int" rather than char(22) (CCN's are not always 16 digits, some can be 20 or 22).
you should probably switch the column type to make sure the data inserts correctly to something like
ALTER TABLE mytable MODIFY myfield char(22);
Say I have a mysql table, and I have a column of type enum and that column has defined set of values like enum('a','b','c','d').
How would i add a value of 'e' to this set using alter table statement?
And I want to append the new value to the end of it using CONCAT.
Unfortunately, you need to re-list all the existing enum values when adding a new value to the the enum.
ALTER TABLE mytable MODIFY COLUMN mycolumn ENUM('a','b','c','d','e');
You don't really want to use CONCAT() in this situation.
If you want to add default value and also want after a specific column for enum, try this query:
Alter table `your_table`
Add column `visible_on` enum('web','mobile','both') default 'both'
After `your_column`;
I have a table and I have added a new column to it. I need to populate this new column and also set the default value for it.
The value of the new col is obtained by concatenating two strings based on the values of other columns:
the first string is the sum COL_1 + 10000
the second string is a obtained by stripping everything but the alphanumerics in COL_2
Update TABLE set NEW_COL = CONCAT ((SUM (10000 + COL_1)), (preg_replace('/[\s\W]+/','',COL_2)))
This will be the default value for the column
The reason your update is failing is that preg_replace() is not a valid MySQL function. That's a PHP function. Here's a relevant question that addresses that functionality in MySQL:
How to do a regular expression replace in MySQL?
I need to add a "0" before a integer field in a MYSQL table. How do I update those fields so they have a ) added to them? Thanks.
You can't unless you change the column type to string. However you can add the zero when reading the field:
SELECT CONCAT( '0', int_col ) FROM ....
If you want to format the output of all integer values in one column to a fixed column size, say 5, with leading zeroes for small numbers, use an ALTER TABLE statement like:
ALTER TABLE table MODIFY `col` INT(5) ZEROFILL;
Refer to MySQL 5.1 Reference Manual.