Incorrect DECIMAL value when inserting MySQL

只谈情不闲聊 提交于 2019-12-13 07:26:54

问题


The error I'm getting is:

ERROR 1366 (HY000): Incorrect DECIMAL value: '0' for column '' at row -1

I'm trying to normalize a database and ensuring that the data types are correct. Inserting data from BASE_TABLE into a new table named Inventors.

This is the query I'm using to insert. If I take a single row manually from the select query it can correctly insert into the Inventors table.

However, running the query like this I instantly get the error above.

INSERT INTO
    Inventors(ID,Firstname,Middlename,Lastname,Country,Latitude,Longitude)
SELECT DISTINCT
    Inventor_ID as ID,
    Firstname,
    Middlename,
    Lastname,
    Country,
    cast(Latitude as decimal(11,6)) as Latitude,
    cast(Longitude as decimal(11,6)) as Longitude
FROM
    BASE_TABLE

Here is the row which fails to insert in the select query:

ID          Firstname   Middlename  Lastname    Country     Latitude    Longitude
04308666-3  RICHARD     RICHARD     JUNG                    0.000000    0.000000

Inventors create query:

CREATE TABLE `Inventors` (
  `ID` varchar(55) NOT NULL DEFAULT '',
  `Firstname` varchar(255) DEFAULT NULL,
  `Middlename` varchar(255) DEFAULT NULL,
  `Lastname` varchar(255) DEFAULT NULL,
  `Country` varchar(255) DEFAULT NULL,
  `Latitude` decimal(11,8) DEFAULT NULL,
  `Longitude` decimal(11,8) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

BASE_TABLE create query:

CREATE TABLE `BASE_TABLE` (
  `Firstname` varchar(255) DEFAULT NULL,
  `Middlename` varchar(255) DEFAULT NULL,
  `Lastname` varchar(255) DEFAULT NULL,
  `Country` varchar(255) DEFAULT NULL,
  `Zipcode` varchar(255) DEFAULT NULL,
  `Latitude` varchar(255) DEFAULT NULL,
  `Longitude` varchar(255) DEFAULT NULL,
  `InvSeq` int(11) DEFAULT NULL,
  `Patent` varchar(255) DEFAULT NULL,
  `AppYear` year(4) DEFAULT NULL,
  `ApplyYear` year(4) DEFAULT NULL,
  `PubYear` year(4) NOT NULL,
  `AppDate` varchar(255) DEFAULT NULL,
  `Assignee` varchar(255) DEFAULT NULL,
  `AsgNum` varchar(255) DEFAULT NULL,
  `Class` varchar(255) DEFAULT NULL,
  `Coauthor` varchar(255) DEFAULT NULL,
  `Invnum` varchar(255) DEFAULT NULL,
  `Invnum_N` varchar(255) DEFAULT NULL,
  `Record_ID` varchar(255) NOT NULL,
  `Inventor_ID` varchar(255) DEFAULT NULL,
  `Match_Level` tinyint(4) DEFAULT NULL,
  `Company_ID` varchar(255) DEFAULT NULL,
  `Classification` varchar(255) DEFAULT NULL,
  `Citing` mediumint(9) DEFAULT NULL,
  `Cited` mediumint(9) DEFAULT NULL,
  PRIMARY KEY (`Record_ID`),
  KEY `Inventor_ID` (`Inventor_ID`),
  KEY `Patent` (`Patent`),
  KEY `Patent_2` (`Patent`,`Inventor_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Can anyone help me on this issue?


回答1:


I found the problem. Latitude (VARCHAR) and Longitude (VARCHAR) in BASE_TABLE had the value of an empty string ''. This caused MySQL to cast to an incorrect value for some reason.

I solved this by replacing the empty string with NULL in the select query (See below).

INSERT INTO
    Inventors
SELECT DISTINCT
    Inventor_ID as ID,
    Firstname,
    Middlename,
    Lastname,
    Country,
    IF(Latitude='',NULL,CAST(Latitude as decimal(11,6))) as Latitude,
    IF(Longitude='',NULL,CAST(Longitude as decimal(11,6))) as Longitude
FROM
    BASE_TABLE



回答2:


I would have added this as a comment but don't have the required rep, so my apologies for that.

I ran into the exact same issue and behavior that Philip did.

To answer philipxy's (now 1.75 year old) question, Philip means that:

  1. When the SELECT statement is executed by itself, it works without any errors - the CAST succeeds in turning an empty string ('') into 0.000000 and that is reflected in the query result set.
  2. When the SELECT statement is executed as part of an encompassing INSERT INTO statement, CAST fails to turn that exact same empty string into a decimal value and Error 1366 occurs.

This is what he correctly calls "ambiguous" because the CAST is working differently in those two different use cases, which is confusing at best. It's also odd that the error message doesn't include a column name and that the row is listed as -1.

I think there might be a bug to be addressed here because CAST should work the same way no matter the scenario.

Luckily, Philip's own Answer also worked for me, so thanks, Philip!



来源:https://stackoverflow.com/questions/43140136/incorrect-decimal-value-when-inserting-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!