Error Code 1292 - Truncated incorrect DOUBLE value - Mysql - mysql

I am not sure what is this error!
#1292 - Truncated incorrect DOUBLE value:
I don't have double value field or data!
I have wasted a whole hour trying to figure this out!
here is my query
INSERT INTO call_managment_system.contact_numbers
(account_id, contact_number, contact_extension, main_number, created_by)
SELECT
ac.account_id,
REPLACE(REPLACE(REPLACE(REPLACE(ta.phone_number, '-', ''), ' ', ''), ')', ''),'(','') AS Phone,
IFNULL(ta.ext, '') AS extention,
'1' AS MainNumber,
'2' AS created_by
FROM
cvsnumbers AS ta
INNER JOIN accounts AS ac ON ac.company_code = ta.company_code
WHERE
LENGTH(REPLACE(REPLACE(REPLACE(REPLACE(ta.phone_number, '-', ''), ' ', ''), ')', ''),'(','') ) = 10
here is my show create table for the table which the results are going into
CREATE TABLE `contact_numbers` (  
`number_id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
`account_id` int(10) unsigned NOT NULL DEFAULT '0',  
`person_id` int(11) NOT NULL DEFAULT '0',  
`contact_number` char(15) NOT NULL,  
`contact_extension` char(10) NOT NULL DEFAULT '',  
`contact_type` enum('Primary','Direct','Cell','Fax','Home','Reception','Office','TollFree') NOT NULL DEFAULT 'Primary',  
`contact_link` enum('Account','PDM','Other') NOT NULL DEFAULT 'Account',  
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0 = inactive, 1=active',
 `main_number` tinyint(1) NOT NULL DEFAULT '0' COMMENT '1 = main phone number',  
`created_on` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,  
`created_by` int(11) NOT NULL,  
`modified_on` datetime DEFAULT NULL,  
`modified_by` int(11) NOT NULL DEFAULT '0',  
PRIMARY KEY (`number_id`),  
KEY `account_id` (`account_id`),  
KEY `person_id` (`person_id`)
) ENGINE=InnoDB AUTO_INCREMENT=534 DEFAULT CHARSET=utf8

This message means you're trying to compare a number and a string in a WHERE or ON clause. In your query, the only potential place where that could be occurring is ON ac.company_code = ta.company_code; either make sure they have similar declarations, or use an explicit CAST to convert the number to a string.
If you turn off strict mode, the error should turn into a warning.

I corrected this error as there was a syntax error or some unwanted characters in the query, but MySQL was not able to catch it. I was using and in between multiple fields during update, e.g.
update user
set token='lamblala',
accessverion='dummy' and
key='somekey'
where user = 'myself'
The problem in above query can be resolved by replacing and with comma(,)

I was facing the same issue. Trying to compare a varchar(100) column with numeric 1. Resulted in the 1292 error. Fixed by adding single quotes around 1 ('1').
Thanks for the explanation above

TL; DR
This might also be caused by applying OR to string columns / literals.
Full version
I got the same error message for a simple INSERT statement involving a view:
insert into t1 select * from v1
although all the source and target columns were of type VARCHAR. After some debugging, I found the root cause; the view contained this fragment:
string_col1 OR '_' OR string_col2 OR '_' OR string_col3
which presumably was the result of an automatic conversion of the following snippet from Oracle:
string_col1 || '_' || string_col2 || '_' || string_col3
(|| is string concatenation in Oracle). The solution was to use
concat(string_col1, '_', string_col2, '_', string_col3)
instead.

I've seen a couple cases where this error occurs:
1. using the not equals operator != in a where clause with a list of multiple or values
such as:
where columnName !=('A'||'B')
This can be resolved by using
where columnName not in ('A','B')
2. missing a comparison operator in an if() function:
select if(col1,col1,col2);
in order to select the value in col1 if it exists and otherwise show the value in col2...this throws the error; it can be resolved by using:
select if(col1!='',col1,col2);

When I received this error I believe it was a bug, however you should keep in mind that if you do a separate query with a SELECT statement and the same WHERE clause, then you can grab the primary ID's from that SELECT: SELECT CONCAT(primary_id, ',')) statement and insert them into the failed UPDATE query with conditions -> "WHERE [primary_id] IN ([list of comma-separated primary ID's from the SELECT statement)" which allows you to alleviate any issues being caused by the original (failed) query's WHERE clause.
For me, personally, when I was using quotes for the values in the "WHERE ____ IN ([values here])", only 10 of the 300 expected entries were being affected which, in my opinion, seems like a bug.

In my case it was a view (highly nested, view in view) insertion causing the error in mysql-5.6:
CREATE TABLE tablename AS
SELECT * FROM highly_nested_viewname
;
The workaround we ended up doing was simulating a materialized view (which is really a table) and periodically insert/update it using stored procedures.

Had this issue with ES6 and TypeORM while trying to pass .where("order.id IN (:orders)", { orders }), where orders was a comma separated string of numbers. When I converted to a template literal, the problem was resolved.
.where(`order.id IN (${orders})`);

If you have used CHECK CONSTRAINT on table for string field length
e.g: to check username length >= 8
use:
CHECK (CHAR_LENGTH(username)>=8)
instead of
CHECK (username>=8)
fix the check constraint if any have wrong datatype comparison

If you don't have double value field or data, maybe you should try to disable sql strict mode.
To do that you have to edit "my.ini" file located in MySQL installation folder, find "Set the SQL mode to strict" line and change the below line:
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
to this, deleting "STRICT_TRANS_TABLES"
# Set the SQL mode to strict
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
After that, you have to restart MySQL service to enable this change.
To check the change, open the editor an execute this sql sentence:
SHOW VARIABLES LIKE 'sql_mode';
Very Important: Be careful of the file format after saving. Save it as "UTF8" and don't as "TFT8 with BOM" because the service will not restart.

Related

WHERE id='2bar' for INT(11) field returns false positive id=2

How to make sure that MySQL does not find false positives when a alphabetic character is appended to a integer value?
Turns out that given this table:
CREATE TABLE `mytable` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `mytable`
ADD PRIMARY KEY (`id`);
And this query:
SELECT * FROM `mytable` WHERE `id`='2'
That works OK and shows a result where id=2. But i noticed that a string value of '2abc' in the query also returns the that row: a false positive. I'd rather have the query fail and signal that it could not find that id in mytable..
This could be circumvented by making sure the value is a valid integer before running the query, but i'd rather keep it string-compatible for future purposes.
This is well documented in the doc
https://dev.mysql.com/doc/refman/5.7/en/type-conversion.html
Check for line
The reason for this is that there are many different strings that may
convert to the value 1, such as '1', ' 1', or '1a'.
So without explicit type cast it will convert the string to integer and in your case 2abc becomes 2 and 2=2 => true, this is called silent conversion
select '2ab'+0; => 2
but
select 'ab2'+0 => 0

MySQL, XML boolean values, and integers: Incorrect integer value:

Im working on grabbing xml data from ebay and putting the results into MySQL, Grabbing the data works fine, however inputting to database fails due to an incorrect integer value for a couple of the xml tags values.
The xml tag value is the word "true" (without the quotes), and this is the db sql:
CREATE TABLE ebay_categories (
CategoryID int(10) NOT NULL default '0',
CategoryLevel int(5) NOT NULL default '0',
CategoryName varchar(120) NOT NULL default '',
CategoryParentID int(10) NOT NULL default '0',
LeafCategory int(1) NOT NULL default '0',
AutoPayEnabled int(1) NOT NULL default '0',
Expired int(1) NOT NULL default '0',
IntlAutosFixedCat int(1) NOT NULL default '0',
Virtual int(1) NOT NULL default '0',
LSD int(1) NOT NULL default '0',
ORPA int(1) NOT NULL default '0',
PRIMARY KEY (CategoryID),
KEY catlevel (CategoryLevel),
KEY parent (CategoryParentID),
KEY ape (AutoPayEnabled),
KEY expired (Expired),
KEY IAFC (IntlAutosFixedCat),
KEY virtual (Virtual),
KEY lsd (LSD),
KEY orpa (ORPA),
KEY leaf (LeafCategory)
) TYPE=MyISAM;
i have tried int, tinyint, Boolean (resorts to tinyint) to no avail and still get this issue. Theres nothing wrong with the db connection as i ran a test using varchar as the int type for the LeafCategory and others and everything worked ok.
is theres something i can do without resorting to searching and replacing via regex before db insertion?
edit:
$query = "INSERT INTO `ebay_categories` (`CategoryID`, `LeafCategory`)VALUES ('$xmlCategoryID', '$xmlLeafCategory')";
if (mysqli_query($link, $query)) {
echo "Successfully inserted " . mysqli_affected_rows($link) . " row";
} else {
echo "Error occurred: " . mysqli_error($link);
}
The SQL statement unwrapped from client code is:
INSERT INTO `ebay_categories`
(`CategoryID`, `LeafCategory`)
VALUES
('$xmlCategoryID', '$xmlLeafCategory')";
The error you have is telling you at least one record in your XML has something other than a valid integer for either $xmlCategoryID or $xmlLeafCategory.
What to do?
A. You could have your error message display the offending data, something like this:
echo "Error occurred: " . mysqli_error($link);
echo "Bad data was either ##$xmlCategoryID## or ##$xmlLeafCategory##.";
Notice that I used ##$value## so you can detect empty strings in your error messages. You probably should do this.
B. You could try changing your column definitions for those columns to remove the NOT NULL declaration. If in fact one of those values is empty, this may fix your problem.
C. It's possible that you need bigint values for this information. That is, they could be very large numbers.
D. If you don't care enough about null or bad values to bother to detect them you could try this.
INSERT INTO `ebay_categories`
(`CategoryID`, `LeafCategory`)
VALUES
(CAST(IFNULL('$xmlCategoryID',-1) AS INT)
(CAST(IFNULL('$xmlLeafCategory',-1) AS INT)
This will turn null values into -1 and non-integer values into 0.
Edit: Oh, I understand now. Your leafCategory item isn't a number in XML, it's a true/false/empty value. Presumably false and empty mean the same thing.
Try this, using a SQL case statement to translate true/other to 1/0.
INSERT INTO `ebay_categories`
(`CategoryID`, `LeafCategory`)
VALUES (
'$xmlCategoryID',
CASE '$xmlLeafCategory' WHEN 'true' THEN 1 ELSE 0 END
)
Finally, danger! You need to use prepared statements, and you need to sanitize these values you're extracting from your XML file. If you don't somebody will be able to trick your software into destroying your database by feeding it a poisoned XML file. Please read up on SQL injection.

Mandatory field mysql

How to make sure that field is mandatory ? Here is what I mean
I have the following mysql table structure:
CREATE TABLE `new` (
`id` int(11) DEFAULT NULL,
`name` int(11) DEFAULT NULL,
`phone` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Here is query with no data for phone
insert into new values(1, 'm', '');
But the query runs fine. What should be done so that mysql returns an error if there is no data for phone field? I can do that validation by php, but I'm curious how to do that in mysql.
Possibly setting the default value of the 'phone' column to NULL would make it fail insertion because it would end up null if you did not specify it.
Otherwise you're going to need to omit the phone column for the default to kick in, say in php you'd use empty($phone) ? null : $phone; or something along those lines.
INSERT INTO new VALUES(1,'m',NULL)
will cause error.
If you want to check whether is the phone number field is a blank string,
you can use a trigger in MySQL.
I haven't tested this, but I have a feeling the '' != null. What happens if you run
insert into new(id, name) values (1, 'test');
I bet you get an insert error...
Anyway, I think its probably better to be validating in PHP than waiting till you get to the database... inserts are expensive...
'' as the 3rd option doesnt make the value of phone null.. It is just equal to a blank string thats all.
if you want to see an error, replace '' with NULL.

MySQL insert query with missing not null fields

I currently trying to use an Object Relational Mapper for CodeIgniter and I'm experiencing something I did not expect.
I have a table with a couple of fields, some of which are NOT NULL. An insert query is which is missing of the NOT NULL fields is generated -- a new row is added but with blanks for those fields.
I did not know MySQL would disregard the NOT NULL fields that aren't present in the query and insert the row anyways. Is there a way to restrict this?
-Edit-
Let me add a few more details and try to explain it a bit more
Here is a sample table:
CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`color` varchar(40) COLLATE utf8_bin DEFAULT '',
`shape` varchar(40) COLLATE utf8_bin NOT NULL,
`size` varchar(40) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
Here is a sample query:
INSERT INTO `test` (`shape`) VALUES ('foo')
I don't have size in my query yet it still adds the row - is this expected?
(The sample query was run in phpMyAdmin)
I believe the accepted answer is incorrect, given the question's test INSERT statement. It looks to me like MySQL's "strict mode" is turned off for this table or database. From the docs:
Strict mode controls how MySQL handles input values that are invalid or missing... A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition...
If you are not using strict mode (that is, neither STRICT_TRANS_TABLES nor STRICT_ALL_TABLES is enabled), MySQL inserts adjusted values for invalid or missing values and produces warnings.
You can find out how your database is running with these queries:
SELECT ##global.sql_mode;
SELECT ##session.sql_mode;
Changing these values is discussed here: https://stackoverflow.com/a/5273824/27846
Empty string is not the same thing as NULL. Perhaps ORM inserts just '' for those fields.
Not a codeigniter dev, but I would hazard a guess that the issue is your ORM is passing blank values on to the database, I would check your logs to verify this and if its the case, check your ORM if it has some validation options.

`id` int(10) NOT NULL auto_increment

Some scripts I migrated are doing lots of
INSERT INTO `table` ( `id` , `fld2` , `fld3`) VALUES ( '', 'v2', 'v3')
id is defined as: int(10) NOT NULL auto_increment.
My database/mysql server (5.1.57) throws error:
1366 - Incorrect integer value: '' for column 'id' at row 1
'' was being accepted without syntax error and did auto-increment the integer field to the next number on the original server (5.1.52). Any idea what could be the difference in the mysql server setup? It couldn't be the version difference, both are 5.1.xx?
Since id is auto_increment you should not mention it in your insert query :
INSERT INTO table (fld2 , fld3) VALUES ('v2', 'v3')
And besides you are inserting string in your id thus in case it was not auto_increment you would get the same error anyway.
Looks like the current server's default settings differ from the previous one. Have a look at MySQL's documentation regarding the strict mode settings.