MySql insert statement to binary datatype? - mysql

I am using MySQL database.
I have one table having column with datatype binary(16).
I need help with the insert statement for this table.
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9e-efdf-b449');
Error : Lookup Error - MySQL Database Error: Data too long for column 'distid' at row 1
How to resolve this issue?

You should remove the hyphens to make the value match the length of the field...
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9eefdfb449');
Also, MySQL standard is to use this notation to denote the string as binary... X'9fad5e9eefdfb449', i.e.
insert into assignedresource values (X'9fad5e9eefdfb449');

Well, assuming that you want to strictly insert a hexadecimal string, first you need to remove the dashes and then "unhex" your string before inserting it into a binary(16) data type column, the code would go like this:
INSERT INTO `assignedresource` VALUES(UNHEX(REPLACE('9fad5e9e-efdf-b449','-','')));
Also... the "usable" data you are inserting is actually 8 bytes after undashing it, so binary(8) would do fine if you plan on not storing the dashes.

You can strip the hyphens and perpend 0x to the value unquoted, like this:
insert into assignedresource values (0x9fad5e9eefdfb449);
As well as, as this (mentioned in other answers):
insert into assignedresource values (X'9fad5e9eefdfb449');
Both are valid notation for a hexadecimal literal.

Your string is 18 char long, change the database
CREATE TABLE `assignedresource` (
`distid` binary(18) NOT NULL
)

Related

Error while encrypting the password in mysql

I created table like this
create table my(username varchar(23),
rolnumber varchar(12),
pword varchar(50));
Trying to insert values like this
insert into my
values ('sandeep','10r81a0229',aes_encrypt('ori12','sand12'));
getting error:
1366 incorrect string value
AES_ENCRYPT() returns binary data which can't be stored in VARCHAR field. You should use appropriate field type, like blob, instead.
From manual:
if you want to store binary values such as results from an encryption or compression function that might contain arbitrary byte values, use a BLOB column rather than a CHAR or VARCHAR column

Converting VARCHAR to DECIMAL values in MySql

I have imported a CSV file that contains string values (eg.eating) and floating values (eg. 0.87) into a table in my phpMyAdmin database. After I get ride of all the string values and retain only the rows that have the decimal values, I need to convert such values from VARCHAR to DECIMAL/FLOAT so that I can perform a MAX() on this attribute.
How do I do this? Each time I try doing this through the GUI in phpMyAdmin, all my values are automatically rounded off to 0 and 1s.
Please help me!
Without Converting you can find Maximum using this query
select max(cast(stuff as decimal(5,2))) as mySum from test;
check this SQLfiddle
your demo table:
create table test (
name varchar(15),
stuff varchar(10)
);
insert into test (name, stuff) values ('one','32.43');
insert into test (name, stuff) values ('two','43.33');
insert into test (name, stuff) values ('three','23.22');
Your Query:
For SQL Server, you can use:
select max(cast(stuff as decimal(5,2))) as mySum from test;
Be aware that if you convert from VARCHAR to DECIMAL and do not specify a precicision and maximum number of digits (i.e. DECIMAL instead of DECIMAL(5,2)) MySQL will automatically round your decimals to integer values.
I think you need to try doing something like this on your MySQL if you have admin privilege on your MySQL.
ALTER TABLE tablename MODIFY columnname DECIMAL(M,D)
for the M,D variables, read this - http://dev.mysql.com/doc/refman/5.0/en/fixed-point-types.html
And MySQL should be able to automatically converting a text to a numeric. Just that the data type in MySQL might not be a decimal yet that's why you can't store any decimal.
Hope it may help someone
select convert( if( listPrice REGEXP '^[0-9]+$', listPrice, '0' ), DECIMAL(15, 3) ) from MyProduct WHERE 1

How to insert * symbol in mysql using php

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);

Convert char(5) to tinyint(4) in mysql

I'm trying to insert rows from one table to the other. In the first table, the datatype of one column is char(5), but the same column has tinyint(4) datatype in the second table. When i run the insert query, it says
Incorrect integer value: '' for column 'x' at row 258
I cannot alter or modify the datatype now as it violates some constraints. Is there a way to use cast or convert char to tinyint?
Thanks.
You probably want something like this:
INSERT INTO newtable
SELECT CASE WHEN x = '' THEN 0 ELSE x END
FROM oldtable
I'm assuming that you want blanks to turn into zeros? If not, then provide the integer value you want blanks to have.
If there are other exceptions, use more alternatives in the CASE expression.

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.