PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 26, 2022

[FIXED] How can I avoid warnings when importing numbers by replacing comma's with a dot in a MySQL table?

 April 26, 2022     csv, import, mysql, replace, warnings     No comments   

Issue

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

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, ',', '.');


Answered By - Akina
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing