Importing space-delimited data into MySQL - mysql

I have file in the following format:
`e00` `e01` `e02` `e03`
`e10` `e11` `e12` `e13
Trying to import the data with
LOAD DATA INFILE 'file' INTO TABLE 'foo' FIELDS TERMINATED BY ' ' ENCLOSED BY '`'
only seems to get the first 3 fields of each line. Is there a way to load the data without altering the file format?

Let's all jump in the way-back machine to answer a 5-year old question!
The fact that the last item is not being loaded is a big hint. According to the manual:
If LINES TERMINATED BY is an empty string and FIELDS TERMINATED BY is
nonempty, lines are also terminated with FIELDS TERMINATED BY.
So, it's looking for a trailing space at the end of the line.
You could add a space to the end of each line in your input file, or try LINES TERMINATED BY '\n'

Related

Can I LOAD DATA with ugly data that has escape characters and quotes?

I have two records for example like this
11,avec myName à EX,Ex,0,2021-06-25
22,"andone \"ttt\"",Ex,0,2021-06-25
I am trying to load this into a table with MySQL with load data, and every time I try it, the quotes get cut off, or the back spaces don't show up.
I need to know if this is even possible. Can those two records go into a table and look exactly like they are in the CSV file?
I am using MySQL and trying
LOAD DATA LOCAL INFILE 'example.csv' INTO TABLE example;
Use the ENCLOSED BY and ESCAPED BY options.
LOAD DATA LOCAL INFILE 'example.csv'
INTO TABLE example
FIELDS OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\';
LOAD DATA LOCAL INFILE 'example.csv' INTO TABLE example
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(col1, col2, col3.col4,col5);
In mysql 8 this will get you an error please read
https://dev.mysql.com/doc/refman/8.0/en/load-data.html#load-data-file-location
Windws uses LINES TERMINATED BY '\r\n'
if the file comes from a linux system use `LINES TERMINATED BY '\n'
it os hard to tell without seeing the file what parameters would help exactly so you should try some variants out`.
also a editor with hexfile capability helps in such cases to analyse the struicture

Is there way to removing spacing in the end of data of columns in the mysql

I have problem to my data that I load to the database via csv file. the column in the csv has no spacing however after I load the data. some of columns has spacing.
This is the script that I used to load the data to my table.
LOAD DATA INFILE 'C:/Users/Buks/Desktop/dates.csv' INTO TABLE cron_db.dates_reference
FIELDS TERMINATED BY ' ' OPTIONALLY ENCLOSED BY '`' LINES TERMINATED BY '\n' IGNORE 1 LINES
(#var1,fiscal_year,fiscal_month,period,week,week2,yearbefore,periodbefore,weekbefore,week2before,ypw)
SET date_record = STR_TO_DATE(#var1,'%c/%e/%Y');
My CSV:
Data Loaded in the table:
It looks like you have defined your column as CHAR(n) - with a fixed length and this forces MySQL to pad with spaces on the right. If this is not your case then you can try with TRIM:
LOAD DATA INFILE 'C:/Users/Buks/Desktop/dates.csv' INTO TABLE cron_db.dates_reference
FIELDS TERMINATED BY ' ' OPTIONALLY ENCLOSED BY '`' LINES TERMINATED BY '\n' IGNORE 1 LINES
(#var1,fiscal_year,fiscal_month,period,week,week2,yearbefore,periodbefore,weekbefore,week2before,#va2)
SET date_record = STR_TO_DATE(#var1,'%c/%e/%Y'), ypw = TRIM(#var2);

MySQL Load Data enclosed character and field terminator on last field

I'm having a seemingly basic problem with loading data to MySQL. I'm using:
LOAD DATA LOCAL INFILE 'C:/.../Contrato.txt'
INTO TABLE schema.contrato
FIELDS TERMINATED BY ';' ENCLOSED BY '|' LINES TERMINATED BY '/r/n' IGNORE 0 LINES;
Each line on the file looks like this, generated by another program:
|abc|;|cde|;|123|;|456|;|name|\r\n
When executing the load, everything seems to load properly, except the very last field. When I look at the table, the last field actually shows the '|' characters around the name. It's not the end of the world, but it's strange that it would do that. As of now, I'm fixing it by adding a ';' right before the \r\n characters.
Is this the way it's supposed to be done?? Why would I need to add the field terminator before the line terminator in order to delete the field enclosers?
I can duplicate the effect on a file with a single line in it, with multiple lines I only get a single entry which has the final column entry of "name| |abc" .
I changed '/r/n' to '\r\n' & the load worked correctly for files with a single entry & multiple entries & the surrounding | were correctly removed
LOAD DATA LOCAL INFILE 'C:/.../Contrato.txt'
INTO TABLE schema.contrato
FIELDS TERMINATED BY ';' ENCLOSED BY '|' LINES TERMINATED BY '\r\n' IGNORE 0 LINES;
The correct MySQL escape character is backslash not forwardslash which is not treated as a special character - so your original code was looking for the 4 character sequence "forwardslash r forwardslash n" to terminate the lines

Loading a file with newline characters in text field

So I have data with in which all the fields are enclosed by quotes and its delimited by pipe. Some fields have html text in them so there are new line characters as part of the field. I want these new line characters to be part of the text field. The data looks something like this:
"abcd"|"1"|""|" abcdegf
"|"abcd"
Also, html data is huge amount of text (sample shows ery less data) and I get the error 'multibyte enlose string not supported". I am on infobright. I am okay even if I can remove those fields from CSV file. They are not needed for analysis. What should be the correct LOAD DATA LOCAL INFILE syntax for this?
I am new to this field, help is greatly appreciated.
load data infile '<file>' into table <table> fields enclosed
by '"' terminated by '|';
LOAD DATA INFILE 'filename' "STR '\r\n'"
APPEND INTO TABLE tablename FIELDS TERMINATED BY "|"
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
Please add the below code to the CTL file -
LOAD DATA INFILE 'filename'
APPEND CONTINUEIF LAST != "|"
INTO TABLE IDP.M_ACTION FIELDS TERMINATED BY "|"
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS (..
The previous answer is not supported on all SQL Loader versions. You can try this solution instead.

Not able to import price column properly in MySQL from a CSV file

I've a csv file containing data in this format:
SATURN,6459,"50,486",27184
I'm using this command to import this file into the table:
LOAD DATA LOCAL INFILE 'D:\\temp.csv' INTO TABLE `test`.`tmp` FIELDS ESCAPED BY '\\'
TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
(`description`,`file_no`,`net`,`run_no`);
all the fields are being imported correctly but the net column always having the data like
50.00 // it should be 50,486
Data type of this column is Decimal(10,2). I've also tried Numeric(10,2) but no luck. Any help is highly appreciated. Thanks a lot in advance.
Please have a try with this one:
LOAD DATA LOCAL INFILE 'D:\\temp.csv' INTO TABLE `test`.`tmp` FIELDS ESCAPED BY '\\'
TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
(`description`, `file_no`, #a_variable, `run_no`)
SET `net` = REPLACE(#a_variable, ',', '');
MySQL cannot deal with commas as 1000 separators when inserting numbers. You need to parse out these commas before loading.
If you need the numbers formatted this way when querying, use FORMAT(). For examples see Apply comma to number field in MySQL