convert decimal and date to LOAD DATA INFILE

Asked

Viewed 116 times

0

I am importing a CVS file into Mysql. I have two columns: a quantidade and the other as data. I am trying to convert to Mysql default. In the database this column is as decimal and date. You are importing all the records, only these two fields are being saved with 0 in quantidade and null as date. I am erring on conversion and not what.

CSV file

nome do material;03B;BC 03;800000235213364;14,000;22/06/2017
nome do material;03B;BC 03;800000235213355;14,000;22/06/2017
nome do material;03B;BC 03;800000235213358;14,000;22/06/2017
nome do material;03B;BC 03;800000235213353;14,000;22/06/2017

Code

mysql_query("LOAD DATA LOCAL INFILE 'C:/wamp/www/transbordo-sap/estoque/Estoque.csv' 
     INTO TABLE deposito FIELDS TERMINATED BY ';' 
     OPTIONALLY ENCLOSED BY '\"' 
     LINES TERMINATED BY '\n'
     (Material,Tipo,Posicao,HU,@Qtdd,@Data)  
     SET Qtdd = REPLACE(REPLACE('@Qtdd', '.', ''), ',', '.'),
         Data = STR_TO_DATE(@Data,'%Y-%m-%d')")or die (mysql_error());

1 answer

1


Try it like this, see if you can fix it:

mysql_query("LOAD DATA LOCAL INFILE 'C:/wamp/www/transbordo-sap/estoque/Estoque.csv' 
     INTO TABLE deposito FIELDS TERMINATED BY ';' 
     OPTIONALLY ENCLOSED BY '\"' 
     LINES TERMINATED BY '\n'
     (Material,Tipo,Posicao,HU,@Qtdd,@Data)  
     SET Qtdd = CAST(@Qtdd as DECIMAL(9,2)),
         Data = STR_TO_DATE(@Data, '%d/%m/%Y')")or die (mysql_error());

I took the test and inserted it normally:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.