patternsqlMinor
csv file with fields having commas in them
Viewed 0 times
filewithfieldscsvhavingthemcommas
Problem
I have a CSV file with fields given as follows.
where number's are numbers and string's are strings. I am trying to read this as follows, I am ignoring first header line
But due to embedded commas in some fields, this command is giving causing to
overlap the fields. How would I correctly do this in MySQL?
number1, string1, "string2, string3", number2, string4, "string5, string6"where number's are numbers and string's are strings. I am trying to read this as follows, I am ignoring first header line
LOAD DATA LOCAL
INFILE 'filename.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ','
IGNORE 1 LINES;But due to embedded commas in some fields, this command is giving causing to
overlap the fields. How would I correctly do this in MySQL?
Solution
You just need to add
Documentation link.
File:
Table and test:
OPTIONALLY ENCLOSED BY '"':LOAD DATA INFILE 'filename.csv' INTO TABLE table_name
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
IGNORE 1 LINES;Documentation link.
File:
n1,s1,s23,n2,s4,s56
1,string1,"str2,str3",2,string4,"str5, str6"Table and test:
mysql> create table tbl_name (
n1 integer,
s1 varchar(10),
s23 varchar(10),
n2 integer,
s4 varchar(10),
s56 varchar(10)
);
Query OK, 0 rows affected (0.06 sec)
mysql> LOAD DATA INFILE 'c:\\temp\\data.txt' INTO TABLE tbl_name
-> FIELDS TERMINATED BY ','
-> OPTIONALLY ENCLOSED BY '"'
-> IGNORE 1 LINES;
Query OK, 1 row affected (0.01 sec)
Records: 1 Deleted: 0 Skipped: 0 Warnings: 0Code Snippets
LOAD DATA INFILE 'filename.csv' INTO TABLE table_name
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
IGNORE 1 LINES;n1,s1,s23,n2,s4,s56
1,string1,"str2,str3",2,string4,"str5, str6"mysql> create table tbl_name (
n1 integer,
s1 varchar(10),
s23 varchar(10),
n2 integer,
s4 varchar(10),
s56 varchar(10)
);
Query OK, 0 rows affected (0.06 sec)
mysql> LOAD DATA INFILE 'c:\\temp\\data.txt' INTO TABLE tbl_name
-> FIELDS TERMINATED BY ','
-> OPTIONALLY ENCLOSED BY '"'
-> IGNORE 1 LINES;
Query OK, 1 row affected (0.01 sec)
Records: 1 Deleted: 0 Skipped: 0 Warnings: 0Context
StackExchange Database Administrators Q#124324, answer score: 8
Revisions (0)
No revisions yet.