'How can I avoid warnings when importing numbers by replacing comma's with a dot in a MySQL table?
I have a table in Mysql that looks like:
CREATE TABLE `overig`.`test`(
`ID` INT(10) AUTO_INCREMENT,
`Order_ID` DECIMAL(10) NOT NULL,
`Price_total` DECIMAL(12,2),
PRIMARY KEY (`ID`)
);
I want to import the following CSV:
Order_ID;Price_total
145131343;118,8
145131349;  
I load the CSV data into the MYSQL table:
LOAD DATA LOCAL INFILE 'test3.csv' INTO TABLE `overig`.`test` FIELDS TERMINATED BY ';' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (
`Order_ID`, @Price_total)
SET 
`Price_total`  = REPLACE(@Price_total, ',', '.');
The response:
Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2  Deleted: 0  Skipped: 0  Warnings: 0
mysql> show warnings;
+---------+------+---------------------------------------------------------------+
| Level   | Code | Message                                                       |
+---------+------+---------------------------------------------------------------+
| Warning | 1366 | Incorrect decimal value: '' for column 'Price_total' at row 2 |
+---------+------+---------------------------------------------------------------+
1 row in set (0.00 sec)
How can I avoid this warning when the Price_total is een empty value?
Solution 1:[1]
I solved it.
Problem is loading ' ' in the SQL table.
When using the IF statement it goes correct without warnings.
I used:
LOAD DATA LOCAL INFILE 'test3.csv' 
INTO TABLE `overig`.`test` 
FIELDS TERMINATED BY ';' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\n' 
IGNORE 1 LINES 
(`Order_ID`, @Price_total)
SET `Price_total` = 
IF(@Price_total = '', NULL, REPLACE(@Price_total, ',', '.'));
Solution 2:[2]
Use implicit convertion.
LOAD DATA LOCAL INFILE 'test3.csv' 
INTO TABLE `overig`.`test` 
FIELDS TERMINATED BY ';' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\n' 
IGNORE 1 LINES 
(`Order_ID`, @Price_total)
SET `Price_total` = 0 + REPLACE(@Price_total, ',', '.');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Apojoost | 
| Solution 2 | Akina | 
