MySQL convert data in float field to decimal field - mysql

I have some data in a MySQL table that was mistakenly stored as a float data type. For example:
7.45
I need to convert that data to a decimal type so that I don't run into rounding errors going forward.
If I do this:
ALTER TABLE `invoice_line`
CHANGE `line_quantity` `line_quantity` decimal(18,6) unsigned NOT NULL AFTER `line_rate`;
I end up with values like this in my table:
70.449997
How can I correctly convert the data over to decimal without mangling the data?

I would use something like mysqldump (or MySQL Workbench has a utility as well) to dump the table. Then do your ALTER and recreate it. Just adding precision is going to introduce errors like this because floating point numbers are generally imprecise anyways.

Related

Converting varbinary to longblob in MySQL

We are storing data in an Innodb table having varbinary column. However, our data size requirement has grown to over 1 MB and hence I converted the column to longblob.
alter table mytable modify column d longblob;
Everything seems to be working as expected after I converted the column. However, I like to know from people who have done it earlier if anything more is required other than just converting column as shown above, especially
is there any MySQL / MariaDB version specific issues with longblob that I should take care of. There is no index on the column.
We use mysqldump to take regular backup. Do we need to change anything since the blob storage mechanism seems to be different than varbinary.
Any other precautions/suggestion.
Thank you for your guidance

convert column to varchar in oracle which is mysql longtext

i have a insert statement pulling data from db link.
insert into table (a,b,c)
select a,b,c from table#mysqldb;
here column c is long type in mysql and in oracle its varchar
i tried to cast as varchar, substr(c, 1,2400), UTL_RAW.CAST_TO_VARCHAR2,dbms_lob.substr
none of them are working on oracle side.
tried cast on mysql read part no use.
Can someone tell me how to do this. Here Iam trying to convert long to varchar. we cannot load as clob as this table is used in many places and we cannot change things at so many places
Thanks.
i had to convert the target column to clob to handle this scenario

MySQL Doesn't accept some data types

MySQL Workbench doesn't allow me to use BLOB() and DATETIME() data types. Im getting this error.
The problem is your data types are incomplete. BLOB and DATETIME (as well as a few other types) allow to specify precisions within parentheses. MySQL Workbench adds empty ones for you, but you have to fill them with a good value. Alternatively, you can just remove them to use the default precision.
Instead of BLOB() and DATETIME() use BLOB and DATETIME respectively

Mysql equivalent of Vertica 'Interval' data type

I am trying migrate a table from Vertica to Mysql.
I noticed that my table has a Vertica datatype interval.
The column details state that data sub type is Interval Day to Second
A sample data looks like 0 00:49:51.267000
I was wondering if there was a mysql equivalent, if not what could be the best possible match to store the data
There is no equivalent that I know of.
I would just store it in a varchar() by the looks of that character string.
However, if you want to investigate further and see what other data types are available to you here is a good place to start dev.mysql
You could use a TIME(6) type and load it with a VARCHAR version of the interval. You then would be able to do queries like:
SELECT TIME_TO_SEC(field) FROM TABLE;
SELECT MICROSECOND(field) FROM TABLE;
Just depends I guess on what you are trying to do with it.

Encrypt a datetime field in mysql server

I have 2 stored procedures Encode,Decode and i want to use this sp to convert my datetime column values (say Dob) to an encrypted date.The problem is that the encrypted format is not in datetime(varbinary) and hence it cant be inserted into that field.Changing the datatype or adding a new column doesn' favour me as my db is a huge one with lots of tables and sps.The steps I use presently is:
declare #datetime
set #datetime='01/02/2008 12:45 PM'
declare #secretDate varchar(400)
declare #date varchar(200)
set #date=(select Convert(varchar(200),#datetime,120)
EXEC #secretDate=dbo.Encode #date
set #date=(select Convert(varchar(200),#secretdate,120))
select Convert(varchar(200),convert(varbinary(MAX),#date)) as EncryptedDate
Any suggestion is appreciated!
You would have to do this change of the column definition in multiple steps.
1) Add a new encryptedDate column set to the encoded value.
2) Drop the existing date column from the table.
3) Rename the encryptedDate to existing date column name.
You may be able to do steps 2 + 3 in one command, but I'm not sure of the syntax.
Any suggestion is appreciated!
This whole thing sounds like a bad idea. If the data is encrypted but the 'Decode' function is a stored procedure in the DB, then the data is effectively not encrypted. Doing this also prevents all data compares from working, which is a Bad Thing.
Why not just encode the data when you read it from the DB if you don't want to present it to users?
Times, and particularly dates have a very unusual, non-linear structure. Even storing dates in structures intended for dates is difficult. If you need to store this data encrypted then don't try to store it in a date / datetime field.