Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LOAD DATA INFILE easily convert YYYYMMDD to YYYY-MM-DD?

Tags:

mysql

Hi I have an INFILE I want to import, but the dates are of the form :

AADR,20120403,31.43,31.43,31.4,31.4,1100
AAU,20120403,2.64,2.65,2.56,2.65,85700
AAVX,20120403,162.49,162.49,154.24,156.65,2200

Is there any easy way to convert the dates to be '2012-04-03' without having to do something like open it first with a perl script, convert the dates, and then writing the file back out again?

TIA !!

like image 846
Don Wool Avatar asked Apr 11 '12 08:04

Don Wool


People also ask

How do I change local Infile in MySQL?

To disable or enable it explicitly, use the --local-infile=0 or --local-infile[=1] option. For the mysqlimport client, local data loading is not used by default. To disable or enable it explicitly, use the --local=0 or --local[=1] option.

What is load data infile?

The LOAD DATA INFILE statement reads rows from a text file into a table at a very high speed. If the LOCAL keyword is specified, the file is read from the client host. If LOCAL is not specified, the file must be located on the server.


1 Answers

This loads and converts in one step, no need for another table. For more information see the manual.

LOAD DATA INFILE 'file.txt'
INTO TABLE t1
FIELDS TERMINATED BY ',' 
(column1, @var1, column3, ...)
SET column2 = STR_TO_DATE(@var1,'%Y%m%d')
like image 122
fancyPants Avatar answered Nov 02 '22 05:11

fancyPants