简体   繁体   中英

Import date from csv file as DATE format in MySQL database

I have a csv file that i want to create a database from. Now everything works fine except for the date that is now stored in a VARCHAR column.

Is it possible to make it so that the date get stored in a DATE column when i import it from my phpMyAdmin? The csv looks like this:

Date,HomeTeam,AwayTeam,FTHG,FTAG,FTR,HTHG,HTAG,HTR
29-09-2017,Excelsior,Vitesse,0,3,A,0,1,A
30-09-2017,Heracles,Feyenoord,2,4,A,0,3,A
30-09-2017,Willem II,Den Haag,1,2,A,0,1,A

You may use LOAD DATA and inline a call to STR_TO_DATE to convert your string dates on the fly:

LOAD DATA LOCAL INFILE 'yourfile.csv'
INTO TABLE yourTable
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n' 
(@Date, HomeTeam, AwayTeam, FTHG, FTAG, FTR, HTHG, HTAG, HTR)
SET Date = STR_TO_DATE(@Date, '%d/%m/%Y');

By the way, you are absolutely making the right design decision by storing your date information as actual dates, and not just as text. Storing dates as text opens the door for problems later on when you actually go to use your database table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM