3

I'm importing a CSV file into Heidi SQL. I've created the table using this code:

create table all_data
    (
        Keyword varchar(1000),
        Position int,
        Previous_Position int,
        Search_Volume int,
        KW_Difficulty float,
        URL varchar(1000),
        Post_title varchar(1000),
        Post_URL varchar(1000),
        Genre varchar(1000),
        Location varchar(1000),
        Avg_Daily_Visitors float,
        pageviews int
        )
;

but in the Avg_Daily_visitors column it has "\N" where there is no value. I've been importing the data with this code:

load data local infile 'C:/filepath.../All_Data.csv'
replace into table all_data
fields terminated by ','
    enclosed by '"'
    escaped by '"'
lines terminated by "\r\n"
ignore 1 rows
set
    Avg_Daily_Visitors = replace(Avg_Daily_Visitors,"\N",0),
    pageviews = replace(pageviews,"\N", 0)
;

but it's not replacing the values with 0, which is what I want to achieve. How do I make Heidi SQL replace "\N" with "0" on import?

Thanks.

jceg316
  • 469
  • 1
  • 9
  • 17

1 Answers1

0

First assign the value you read to a variable, then work on that variable. For this you specify the columns of your destination table, but a variable instead of the column where you want to replace.

load data local infile 'C:/filepath.../All_Data.csv'
replace into table all_data
fields terminated by ','
    enclosed by '"'
    escaped by '"'
lines terminated by "\r\n"
ignore 1 rows
(column_1, column_2, @variable1, @variable2, column_5)
set
    Avg_Daily_Visitors = replace(@variable1,"\N",0),
    pageviews = replace(@variable2,"\N", 0)
;
fancyPants
  • 50,732
  • 33
  • 89
  • 96