Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

mysql - Excel dates cannot be converted to desired format

I am trying to import data from a .csv file into MySQL using:

LOAD DATA LOW_PRIORITY LOCAL INFILE 'file_path_here' 
REPLACE INTO TABLE `pitch_data` 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '
'
IGNORE 1 LINES
(id,AT_BAT_NUMBER,etc.,...,ZONE)
SET GAME_DATE = STR_TO_DATE(@GAME_DATE, '%m/%d/%Y')
;

but the conversion of GAME_DATE from MM/DD/YYYY (in the .csv file) to the required format, YYYY-MM-DD isn't happening (I'm receiving the "Error Code: 1292. Incorrect date value: '10/30/2019' for column 'GAME_DATE' at row 1" message).

I have tried changing the format of the GAME_DATE column manually in an .xlsx file to save and copy to the .csv file, but even in the .xlsx file I can't change the format. Strangely, when I select the "Format Cells" option, it indicates that the data are already in the format, YYYY-MM-DD, but they appear as MM/DD/YYYY to the eye. I have tried a similar approach with the "Text to Columns" wizard, but no luck.

Any help would be appreciated!

added code from comment:

Certainly. Here is a subset of the .csv:

id AT_BAT_NUMBER GAME_DATE 
1  79            10/30/2019 
2  79            10/30/2019

and the SQL code:

LOAD DATA LOW_PRIORITY LOCAL INFILE 'FILE_PATH_TO_CSV' 
REPLACE INTO 
TABLE pitch_data 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '
' 
IGNORE 1 LINES 
(id,AT_BAT_NUMBER,GAME_DATE) 
SET GAME_DATE = STR_TO_DATE(@GAME_DATE, '%m/%d/%Y') 
question from:https://stackoverflow.com/questions/65854633/excel-dates-cannot-be-converted-to-desired-format

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you have to set the user defined variable first like so

LOAD DATA LOW_PRIORITY LOCAL INFILE 'FILE_PATH_TO_CSV' 
REPLACE INTO 
TABLE pitch_data 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '
' 
IGNORE 1 LINES 
(id,AT_BAT_NUMBER,@GAME_DATE) 
SET GAME_DATE = STR_TO_DATE(@GAME_DATE, '%m/%d/%Y') 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...