mysql LOAD DATA INFILE NA to NULL transformation

后端 未结 1 1074
一生所求
一生所求 2021-01-14 15:46

Is there an option in the mysql LOAD DATA INFILE command, to take a .tsv file as input to mysql LOAD DATA INFILE, and transform every \'NA\' field in that file

相关标签:
1条回答
  • 2021-01-14 15:53

    You can use variables:

    LOAD DATA LOCAL INFILE 'file.tsv' INTO TABLE my_table
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    (@col1, @col2, @col3)
    SET
      col1 = CASE WHEN @col1 NOT IN ('NA', 'NaN', '--') THEN @col1 END,
      col2 = CASE WHEN @col2 NOT IN ('NA', 'NaN', '--') THEN @col2 END,
      col3 = CASE WHEN @col3 NOT IN ('NA', 'NaN', '--') THEN @col3 END
    

    usin CASE WHEN like this:

    CASE WHEN @col1 NOT IN ('NA', 'NaN', '--') THEN @col1 END
    

    when the condition is true it will return the actual value of @col1, or NULL otherwise

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