How to convert csv date format to into mysql db

后端 未结 3 702
遇见更好的自我
遇见更好的自我 2020-12-19 15:05

At csv file, the date field is in such format:

2/9/2010 7:32
3/31/2011 21:20

I am using php + mysql for development.

I need to read

相关标签:
3条回答
  • 2020-12-19 15:15

    Use the STR_TO_DATE() function.

    Example

    STR_TO_DATE('3/31/2011 21:20', '%c/%e/%Y %H:%i');
    
    0 讨论(0)
  • 2020-12-19 15:22

    I face the same issue and after little research this is how i resolved it-

    LOAD DATA LOCAL INFILE 'D:/dataupload.csv' INTO TABLE table1 FIELDS TERMINATED BY ',' ENCLOSED BY '' LINES TERMINATED BY '\r\n' (@d1,col2,col3,col4) SET col1 = date_format(str_to_date(@d1, **'%m/%d/%Y'**), **'%Y-%m-%d'**)

    Details:

    1. '%m/%d/%Y' - this is the format of date in my CSV file
    2. '%Y-%m-%d' - this is the mysql format in which i want to convert my CSV field date while inserting data
    3. col1 - is the actual column of my table (having date data type)
    4. @d1 - is the dummy variable to use in set statement, you can take it any variable
    0 讨论(0)
  • 2020-12-19 15:40

    I had the same problem (with DATE) and another solution, is to use the native mysql format YYYYMMDD ie 20120209. I haven't tried with DATETIME but I guess YYYYMMDDhhmmss will work.

    0 讨论(0)
提交回复
热议问题