Avoid splitting data on commas (within quotation marks) in SQL?

39 Views Asked by At

I am trying to load a .csv file into a MySQL table, but I'm running into the following error message: "ERROR 1262 (01000): Row 304 was truncated; it contained more data than there were input columns."

Here is the code I'm using:

LOAD DATA INFILE 'my_taxpayers.csv'
INTO TABLE taxpayers
FIELDS ENCLOSED BY '"'
TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

I've pasted Row 304, the first row that causes a problem, below. The corresponding column headers for the row are

PARCEL, TAXPAYER, ADDRESS_1, ADDRESS_2, CITY, STATE, ZIP, COUNTRY, ROLE_PERCENTAGE and IN_CARE_OF.  
*00.000366,"BUNNEY, GARY LEE",40 E TRENT,,SPOKANE,WA,99202,,100.0,**"CAROL LUNDY, 1ST AMERICAN"***

It appears the data is getting split (which I don't want) when it runs into "CAROL LUNDY, 1ST AMERICAN" and maybe "BUNNEY, GARY LEE". I thought the ENCLOSED BY '"' was supposed to solve that, so I'm stuck.

For more context, here are a few more random rows of the .csv file that DIDN'T cause any issues.

00.000102,ANITA'S DAPPER DOGS,727 E 32ND AVE,,SPOKANE,WA,99203,,100.0,ANITA ANN SYKES
00.000103,"MKY INVESTMENTS, INC",9508 N DIVISION ST,,SPOKANE,WA,99218,,100.0,MARK & KAREN YOO
00.000104,COUNTY RECORD'S PUBLISHING CO,503 E ERMINA AVE,,SPOKANE,WA,99207,,100.0,JILL MARIE BUSWELL
00.000105,DANCAR MECHANICAL INC,311 N HODGES RD,,SPOKANE VALLEY,WA,99016,,100.0,DANIEL F SCHROER
00.000106,ARTHUR ARMS ADULT FAMILY HOME,652 S ARTHUR ST,,SPOKANE,WA,99202,,100.0,ANTHONY R JONES
1

There are 1 best solutions below

0
bobsnyder On

The problem was I needed to be doing

LINES TERMINATED BY \r\n

Adding in the r fixes everything.