Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL load data infile ("NULL" String in CSV for datetime)

Tags:

import

sql

mysql

Hi I am have the following CSV format

id   (tab)  creation_date
-------------------------
1           2012-05-04 15:26:45.000
2           NULL

For the importing I am using:

load data local infile './test.csv' into table TEST fields terminated by '\t'
  lines terminated by '\n'
  IGNORE 1 LINES
    (id,@creation_date)
  SET
    creation_date = nullif(@creation_date,'NULL');

But unfortunately the NULL value (String) is not interpreted and set to a NULL in the column. Instead I get a warning about data truncation and a default date is inserted 0000-00-00....

How can I check: Is String = "NULL" then insert NULL into column?

like image 829
cloudnaut Avatar asked May 06 '26 10:05

cloudnaut


1 Answers

The documentation has rules to how NULL is interpreted in the file depending on you query fields and lines used, in simple terms:

  • If FIELD and LINES is default values in your query use \N (NO QUOTES).
  • If FIELD ENCLOSED BY and/or FIELD ESCAPED BY is used, use the word NULL (NO QUOTES).
  • With fixed-row format (which is used when FIELDS TERMINATED BY and FIELDS ENCLOSED BY are both empty), NULL is written as an empty string i.e. "".

Your use case is the first bullet.

**Read more from the docs in the bottom paragraph Handling of NULL values varies according to the FIELDS and LINES options in use: of the section link above.

like image 172
Hmerman6006 Avatar answered May 08 '26 03:05

Hmerman6006



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!