I'm trying to import a dataset that includes some text fields that are enclosed by double quotes "sometext" but when I try to load the file it still shows the double quotes.
Here is the sql
LOAD DATA LOCAL INFILE '/Users/Carlos/Desktop/smallData.txt'
into table acars
FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(#Date, #Time, flight, n_number, blk_id, msg_label, text_label, msg_text)
SET Date_Time = STR_TO_DATE(CONCAT(#Date, ' ', #Time), '%d/%m/%Y %H:%i');
with this small data sample:
date_dd_mm_yyyy; time_est; flight; n_number; blk_id; msg_label; text_label; msg_text
01/03/2014; 00:02; XA0001; ..VQBZZ; 3; 15; ""; " (2N26451W 81508---100354---(Z"
01/03/2014; 00:04; CO1588; .N71411; 7; H1; "msg to/from terminal"; " #DFB96 0 877"
01/03/2014; 00:12; XA0001; ..VQBZZ; 4; 15; ""; " (2N27038W 82483--- 94423---(Z"
the Problem is that I get this format back for all the lines:
| 2014-03-01 00:02:00 | XA0001 | ..VQBZZ | 3 | 15 | "" | " (2N26451W 81508---100354---(Z" |
basically it just doesn't do the OPTIONALLY ENCLOSED BY '"' part
Related
I am using MySQL to load a CSV file, but there is no quotes for the string column, so I only get the first letter of the value.
1,Toyota Park,Bridgeview,IL,0
2,Columbus Crew Stadium,Columbus,OH,0
3,RFK Stadium,Washington,DC,0
After I use the code here, I got this:
LOAD DATA LOCAL INFILE 'C:\\Users\\tank\\Desktop\\test.csv'
INTO TABLE test
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 0 ROWS;
and I had this table:
+---------------------------------------------+
| ID, Studios, City, State, Open |
+---------------------------------------------+
| '1', 'T', 'B', 'I', '0' |
| '2', 'C', 'C', 'O', '0' |
| '3', 'R', 'W', 'D', '0' |
+---------------------------------------------+
You should use enclosed by:
LOAD DATA LOCAL INFILE 'C:\\Users\\tank\\Desktop\\test.csv'
INTO TABLE test
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
ENCLOSED BY '\"'
ESCAPED BY '\"'
IGNORE 0 ROWS;
I am trying to export some data to csv but facing some issue with the "," delemeter.
I am running the below query:
SELECT c.NAME
FROM company c
WHERE COMPANY_ID = 1
INTO OUTFILE 'E:\\ab.csv' FIELDS OPTIONALLY ENCLOSED BY '' TERMINATED BY ',' ESCAPED BY '' LINES TERMINATED BY '\n';
If my name does not contain any "," then the csv is correctly populated but if my "name" field contains "," then data is split into two rows.
For ex:
c.Name = "Google INC" -> Works fine
c.Name = "Google,INC" -> it creates csv with two different column "Google" and "INC".
Kindly suggest what should I do.
Based on the link at the comment you can try something like this
SELECT c.NAME
FROM company c
WHERE COMPANY_ID = 1
INTO OUTFILE 'E:\\ab.csv' FIELDS OPTIONALLY ENCLOSED BY '' TERMINATED BY '",' ESCAPED BY '' LINES TERMINATED BY '\n';
Marking ", instead ,
I have a csv file which contains a lot of sql keywords. How do i escape the keywords?
sample row from the csv file:
F Aabha Hanjura 115 O/o District Fire Officer Chittor Dist . Chittoor AP 500032 500032 Chittoor AP 9963736976 Ap_Airlt_Pst ANDHRA PRADESH AIRTEL
This is what my import statement looks like:
LOAD DATA INFILE "/home/kannel/2.csv" INTO TABLE number_data COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\n';
Suck with the following:
$loaddata = "LOAD DATA INFILE 'filename.csv'
INTO TABLE tb1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\r\\n'
IGNORE 1 LINES
(
Entity,
HK,
#Period,
)
SET Period = STR_TO_DATE(#Period,'%C%YY%MM')
";
which gives me and sql syntax error near
) SET Period = STR_TO_DATE(#Period,'%C%YY%MM')
Period is a DATE variable. for the period Oct-13 the cvs will show 11310.
tks in advance!
You have a superfluous comma after #Period:
$loaddata = "LOAD DATA INFILE 'filename.csv'
INTO TABLE tb1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\\r\\n'
IGNORE 1 LINES
(
Entity,
HK,
#Period -- , removed here
)
SET Period = STR_TO_DATE(#Period,'%C%YY%MM')
";
However, your date format string is almost certainly incorrect. %C, %YY and %MM are invalid specifiers. See DATE_FORMAT().
I do not believe this question is a duplicate.
I want rows in the body to be "OPTIONALLY ENCLOSED BY" double quotes. Numerical values should not be enclosed. That's easy to do without a header. But when you include a header using UNION, MySQL now treats every column as a string type and encloses all the values in quotes.
You can add a header to SELECT INTO OUTFILE like this:
SELECT "id", "numerical_values", "string_values" #header section of csv
UNION ALL
SELECT `id`, `numerical_values`, `string_values` #body section of csv
INTO OUTFILE "/tmp/values.csv"
FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY "\n"
FROM `values_table`
Again, if I omit the header, only string_values will be enclosed by quotes.
With the header, all columns are seen as string columns and will be enclosed.
I tried this:
SELECT '"', "id", ',"', "numerical_values", ',"', "string_values", '"'
#added quotes/commas to header
UNION ALL
SELECT `id`, ',', `numerical_values`, ',"', `string_values`, '"'
#added commas, and add quotes around string_values
INTO OUTFILE "/tmp/values.csv"
FIELDS TERMINATED BY "" ENCLOSED BY '' #empty values for fields terminated and enclosed
# empty because manually selected in query
LINES TERMINATED BY "\n"
FROM `values_table`
I thought that would work, but I ended up with what looks like a bunch of extra whitespace. This is from an file I just created with the 2nd method. The header row ends at "string7". The 1st line of data ends after "207". I just put part of the 3rd line.
" num1"," string1"," num2"," string2"," num3"," string3"," num4"," string4"," num5"," string5"," num6"," string6"," string7""
33.95 ," 1023 ", 7.50 ," 207-1023 ", 26.95 ,"2 1023 ",23.00 ,"3Wx4Hx4D ",19.00 ,"1023 ", 0.00 ,"UPC: 123456789012 "," 207 "
40.95 ," 1058 ", 9.00 ,
SELECT '"id"', '"numerical_values"', '"string_values"'
UNION ALL
SELECT id, numerical_values, CONCAT('"', REPLACE(string_values, '"', '""'), '"')
INTO OUTFILE '/tmp/values.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '' ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM values_table