简体   繁体   中英

MySQL CSV File import dateTime

new here - be friendly.

I'm importing a CSV file into a mysql table and having a particularly hard time with the datetime type.

An example of my import, the datetime value will be the 4th field in.

3026930,{5849EF5A-210E-497D-948D-0006687AD627},550000,28/01/2013          00:00,1,1,2013,2013/1,SE145DS,S,N,F,3, ,NEW CROSS ROAD, ,LONDON,LEWISHAM,GREATER LONDON,     ,SE145DS,Inner,E09000023,Lewisham,E05000449,New Cross,E02000655,E01003302,E00016694 

My import statement:

    LOAD DATA INFILE '/tmp/house1.csv'
          INTO TABLE housesales
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
 LINES TERMINATED BY '\n'
              IGNORE 1 ROWS
                 SET dateprocessed = STR_TO_DATE(@dateprocessed, '%d/%m/%Y %H:%i');

my table:

+----------------+------------------+------+-----+---------+-------+
| Field          | Type             | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+-------+
| saleid         | int(10) unsigned | NO   | PRI | NULL    |       |
| transactionID  | char(50)         | NO   |     | NULL    |       |
| price          | int(10) unsigned | NO   |     | NULL    |       |
| dateprocessed  | datetime         | NO   |     | NULL    |       |
| quarter        | tinyint(4)       | NO   |     | NULL    |       |
| month          | tinyint(4)       | NO   |     | NULL    |       |
| year           | smallint(6)      | NO   |     | NULL    |       |
| yearmonth      | char(10)         | NO   |     | NULL    |       |
| postcode       | char(10)         | NO   |     | NULL    |       |
| propertytype   | char(3)          | NO   |     | NULL    |       |
| whethernew     | char(3)          | NO   |     | NULL    |       |
| tenure         | char(3)          | NO   |     | NULL    |       |
| address1       | char(50)         | YES  |     | NULL    |       |
| address2       | char(50)         | YES  |     | NULL    |       |
| address3       | char(50)         | YES  |     | NULL    |       |
| address4       | char(50)         | YES  |     | NULL    |       |
| town           | char(20)         | YES  |     | NULL    |       |
| localauthority | char(30)         | YES  |     | NULL    |       |
| county         | char(30)         | YES  |     | NULL    |       |
| recordstatus   | char(2)          | YES  |     | NULL    |       |
| postcodeclean  | char(10)         | YES  |     | NULL    |       |
| innerouter     | char(10)         | YES  |     | NULL    |       |
| boroughcode    | char(20)         | YES  |     | NULL    |       |
| boroughname    | char(30)         | YES  |     | NULL    |       |
| wardcode       | char(20)         | YES  |     | NULL    |       |
| wardname       | char(30)         | YES  |     | NULL    |       |
| msoa11         | char(20)         | YES  |     | NULL    |       |
| lsoa11         | char(20)         | YES  |     | NULL    |       |
| oa11           | char(20)         | YES  |     | NULL    |       |
+----------------+------------------+------+-----+---------+-------+

and finally a row from the imported table:

| 3118826 | {F3F11295-2C5B-44E5-870A-ED719FFB12C9} | 100500 | 0000-00-00 00:00:00 |2 |     5 | 2013 | 2013/5    | E16 1BY  | F            | N          | L      | OCEANIS APARTMENTS, 19     | FLAT 38               | SEAGULL LANE            |                      | LONDON               | NEWHAM                 | GREATER LONDON |              | E16 1BY       | Inner      | E09000025   | Newham               | | E05000478 | Canning Town South             | E02000747 | E01033576 | E00174967

I am also getting out of range warnings which is expected if the datetime column is getting null like values. Could it be the excessive white space?

May thanks Michael

Does your csv file have a header? If it has one just remove it and try again.

I tried this command that I saw in another post and it worked perfectly:

 LOAD DATA LOCAL INFILE ' test.csv'
INTO TABLE  housesales
FIELDS OPTIONALLY ENCLOSED BY '"' TERMINATED BY ','
LINES TERMINATED BY '\n'
(row1,row2,row3,etc...)

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