I am trying to populate a MySQL database field with a binary(16) value of an IP address and I'm getting:
Warning: #1265 Data truncated for column 'IP' at row 1
The binary value I am trying to insert is
00000000000000000000ffff5bd25452
What am I doing wrong?
MySql table schema
Currently you're losing data for using BINARY(16). To insert you need to unhex the value, for example:
INSERT INTO <your-table> (IP) VALUES(UNHEX("00000000000000000000ffff5bd25452"));
To get it back you'll be required to use HEX, to have the original form back:
SELECT HEX(IP) FROM <your-table>;
Edit 1: in apropos of your comment
how can I insert an IP string such as 107.180.58.16
As IPv4 addresses are 4 byte long, same as an INT (UNSIGNED):
Use INET_ATON to convert it to INT, and INET_NTOA to convert it back to IPv4 - for example:
INSERT INTO <your-table> (IP) VALUES (INET_ATON("107.180.58.16"));
SELECT INET_NTOA(IP) FROM <your-table>;
And, for IPv6 addresses i.e. 128 bits you can use 16 bytes binary field, BINARY(16) or VARBINARY(16).
How to store an IP in mySQL
Related
I am trying to clean the telephone numbers of a database so we can easily search for them. In the column TEL we have rows like:
654-598-5487
654.254.2456
(458)-5458789 e.3
I want to copy all those values to a new column where only the numeric characters are transferred:
6545985487
6542542456
45854587893
The new column (TEL_NO_FORMAT) is a big int and it only allows numbers, but if I execute something like this:
UPDATE CLIENTS set `TEL_NO_FORMAT` = `TEL`
It will only transfer the first numeric characters found and ignore the rest:
654
654
NULL
Easiest way is the REGEXP_REPLACE(MySQL 8.0+):
SELECT *, REGEXP_REPLACE(tel_no_format, '[^0-9]','') AS result
FROM clients
Answering the question in an update query
UPDATE CLIENTS SET TEL_NO_FORMAT = REGEXP_REPLACE(TEL, '[^0-9]','');
You should replace the undesired char before
UPDATE CLIENTS set `TEL_NO_FORMAT` = replace(replace(replace(`TEL`, '.',''),'-',''),')','')
because some char (eg '-') are create problem during conversion ..
anyway rember that a big int can't manage properly eventual tel number 0 prefixed eg:
00453778988
I need to strip dots from a columns of IP addresses in MySQL table 'visits', I used the following query, what's wrong with it?
UPDATE 'visits' SET 'IP' = REPLACE('IP', '.', '');
Thanks
Use backticks ` instead of single quotes ' for table and column name
UPDATE `visits` SET `IP` = REPLACE(`IP`, '.', '');
That said, this method may create issues.
For e.g. you have two IPs: 10.1.1.11 and 10.1.11.1
After your update, both will become - 101111 and there is no way to tell which is which.
As #Alex said in the comments, if you want to represent the IP as numeric value, consider INET_ATON() instead, which returns an integer that represents the numeric value of the address in network byte order (big endian).
UPDATE `visits` SET `IP` = INET_ATON(`IP`);
It'll return unique number for an IP.
10.1.1.11 - 167837963
10.1.11.1 - 167840513
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
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
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
)