Why is MySQL not converting my data correctly? - mysql

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

Related

How to alter a table with a column of varchar datatype to Datetime/ Timestamp

I tried changing the datatype from Varchar to Datetime, but it keeps on bringing the following error.
Incorrect datetime value: ' ' for column 'pickup_time' at row 6
Result
I was expecting for the query in the image to work.
To change a type to datetime, all existing values must be able to be cast as datetimes without any error. Find the values that are not, or update them first using str_to_date if it is simply a matter of changing the format.
If there are blank values, you will need to change them to null first.

Data truncated for column?

After changing the data type of a MySql column in order to store Twilio call ids (34 char strings), I try to manually change the data in that column with:
update calls
set incoming_Cid='CA9321a83241035b4c3d3e7a4f7aa6970d'
where id='1';
However I get an error which doesn't make sense seeing as the column's data type was properly modified?
| Level ||| Code | Message
| Warning | 1265 | Data truncated for column 'incoming_Cid' at row 1
Your problem is that at the moment your incoming_Cid column defined as CHAR(1) when it should be CHAR(34).
To fix this just issue this command to change your columns' length from 1 to 34
ALTER TABLE calls CHANGE incoming_Cid incoming_Cid CHAR(34);
Here is SQLFiddle demo
I had the same problem because of an table column which was defined as ENUM('x','y','z') and later on I was trying to save the value 'a' into this column, thus I got the mentioned error.
Solved by altering the table column definition and added value 'a' into the enum set.
By issuing this statement:
ALTER TABLES call MODIFY incoming_Cid CHAR;
... you omitted the length parameter. Your query was therefore equivalent to:
ALTER TABLE calls MODIFY incoming_Cid CHAR(1);
You must specify the field size for sizes larger than 1:
ALTER TABLE calls MODIFY incoming_Cid CHAR(34);
However I get an error which doesn't make sense seeing as the column's data type was properly modified?
| Level | Code | Msg | Warn | 12 | Data truncated for column 'incoming_Cid' at row 1
You can often get this message when you are doing something like the following:
REPLACE INTO table2 (SELECT * FROM table1);
Resulted in our case in the following error:
SQL Exception: Data truncated for column 'level' at row 1
The problem turned out to be column misalignment that resulted in a tinyint trying to be stored in a datetime field or vice versa.
In my case it was a table with an ENUM that accepts the days of the week as integers (0 to 6). When inserting the value 0 as an integer I got the error message "Data truncated for column ..." so to fix it I had to cast the integer to a string. So instead of:
$item->day = 0;
I had to do;
$item->day = (string) 0;
It looks silly to cast the zero like that but in my case it was in a Laravel factory, and I had to write it like this:
$factory->define(App\Schedule::class, function (Faker $faker) {
return [
'day' => (string) $faker->numberBetween(0, 6),
//
];
});
I had the same problem, with a database field with type "SET" which is an enum type.
I tried to add a value which was not in that list.
The value I tried to add had the decimal value 256, but the enum list only had 8 values.
1: 1 -> A
2: 2 -> B
3: 4 -> C
4: 8 -> D
5: 16 -> E
6: 32 -> F
7: 64 -> G
8: 128 -> H
So I just had to add the additional value to the field.
Reading this documentation entry helped me to understand the problem.
MySQL stores SET values numerically, with the low-order bit of the
stored value corresponding to the first set member. If you retrieve a
SET value in a numeric context, the value retrieved has bits set
corresponding to the set members that make up the column value. For
example, you can retrieve numeric values from a SET column like this:
mysql> SELECT set_col+0 FROM tbl_name; If a number is stored into a
If a number is stored into a SET column, the bits that are set in the
binary representation of the number determine the set members in the
column value. For a column specified as SET('a','b','c','d'), the
members have the following decimal and binary values.
SET Member Decimal Value Binary Value
'a' 1 0001
'b' 2 0010
'c' 4 0100
'd' 8 1000
If you assign a value of 9 to this column, that is 1001 in binary, so
the first and fourth SET value members 'a' and 'd' are selected and
the resulting value is 'a,d'.
when i first tried to import csv into mysql , i got the same error , and then i figured out mysql table i created doesn't have the character length of the importing csv field ,
so if it's the first time importing csv
its a good idea to give more character length .
label all fields as varchar or text , don't blend int or other values.
then you are good to go.
Check whether you are using the 'enum' datatype.If you are using 'enum' datatype then the items inside the enum datatype should be exactly match with the data which you are entering.Ex.
You are taking the enum datatype like this:
enum('book',stationery','others')
then when you are inserting the data into the database you have to do like this:
INSERT INTO database_name.table_name (column1,...columnn) VALUES().
THE value should include same items which you are mentioned within the bracket of enum datatype.
my issue was I used single quote instead of double quotes which add extra words in Boolean column
I Faced the same issue .In my case i was inserting a empty string for a numeric column.But by inserting a numeric value in string form for same column it got resolved e.g '12.56' --> numeric column will work but '' --> numeric column will give the above mentioned error.
Note: For numeric columns in MySql we can pass values in double quotes also .
In some cases this can be result of wrong type of input. For example, you have a column decimal(10,2) and the input isn't sanitized and is 7,7 but should be 7.7.

Why is AES_DECRYPT returning null?

I've found similar questions, but no clear answer for this question. I have this table:
CREATE DATABASE testDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE testTable
(
firstName binary(32) not null,
lastName binary(32) not null
/* Other non-binary fields omitted */
)
engine=INNODB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
This statement executes just fine:
INSERT INTO testTable (firstName) VALUES (AES_ENCRYPT('Testname', 'test'));
But, this returns NULL:
SELECT AES_DECRYPT(firstName, 'test') FROM testTable;
Why does this return NULL?
Fwiw, this returns "testValue" as expected:
SELECT AES_DECRYPT(AES_ENCRYPT('testValue','thekey'), 'thekey');
The answer is that the columns are binary when they should be varbinary. This article explains it:
Because if AES_DECRYPT() detects invalid data or incorrect
padding, it will return NULL.
With binary column types being fixed length, the length of the input value must be known to ensure correct padding. For unknown length values, use varbinary to avoid issues with incorrect padding resulting from differing value lengths.
When you insert binary data into a VARCHAR field there are some binary characters that a VARCHAR can't handle and they will mess up in the inserted value. And then the inserted value will not be the same when you retrieve it.
1.select hex(aes_encrypt(file,'key'));
2.select aes_decrypt(unhex(file),'key');
Check if type of your field is blob instead binary(32)
Did you try different values other than 'Testname'?
Do other values work?
I ask because I had a situation while testing 2 test credit card numbers where one decrypted fine and the other returned null.
The answer was to hex and unhex as suggested by "abhinai raj"

mysql syntax for populating a column of an existing table and setting a default value for said 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?

Prepend a digit to the beginning of a field

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.