sql loader control file multiple control - sql-loader

I have raw data like this:
2002-3,X52.9418.001 , Abraham, Kennedy James, 111111111, 2125551234,ak123n#aol.com, 123 East 14th Street,, New York, NY,
2002,X52.9418.001 , Richard, Roberts, 111111112, 2125554321,rr345#aol.com, 123 East 14th Street, Apt.6 , New York, NY, 10010, United States, English
2002-4,X52.9418.001 , Katherine Ray Baird , 111111113, (212) 555 3456,kbilu#aol.com, 123 East 14th Street,, New York, New York, 10010-, ,
...
I need to load the raw data into the Oracle database using Sql Loader. I create this control file:
load data
infile '/home/l/liuh/rawdata.txt'
badfile '/home/l/liuh/badfile.bad'
discardfile '/home/l/liuh/discardfile.discard'
into table students
trailing nullcols (
Year INTEGER EXTERNAL terminated by "-",
Semester terminated by ",",
department terminated by ".",
course_number terminated by ".",
session_number terminated by ",",
first_name CHAR(10) terminated by " ",
Middle_name CHAR(15) terminated by ",",
last_name CHAR(15) terminated by ",",
id INTEGER EXTERNAL terminated by ",",
area_code terminated by " ",
phone_number terminated by ",",
email_address CHAR terminated by ",",
Street1 terminated by "," ,
street2 terminated by "," ,
city terminated by "," ,
State_prov terminated by "," ,
postal_code terminated by "," ,
country terminated by "," ,
language terminated by ","
)
But I got nothing into the database. Can somebody help me with the control file? I fount it difficult to control the data. Thanks.

Related

Mysql csv file selecting multiple value in one column

,PMID,LastName,ForeName,Initials,Affiliation
0,1,"['Makar', 'McMartin', 'Palese', 'Tephly']","['A B', 'K E', 'M', 'T R']","['AB', 'KE', 'M', 'TR']",
,,,,,
enter image description here
this is the csv file
I want to divide this to look like this
PMID Name Affiliation
1 Makar_A B_AB
1 MCMartin_K E_KE
1 Palese_M_M
1 Tephly_T R_TR
below is the code I wrote in mysql workbench
DROP TABLE IF EXISTS AuthorTBL;
CREATE TABLE IF NOT EXISTS AuthorTBL
( PMID varchar(100),
LastName varchar(50),
ForeName varchar(50),
Initials varchar(50),
Affiliation varchar(250)
);
LOAD DATA INFILE 'abcd.csv'
INTO TABLE AuthorTBL
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(#num, #PMID, #LastName, #ForeName, #Initials, #Affiliation)
SET
PMID = #PMID,
LastName = #LastName,
ForeName = #ForeName,
Initials = #Initials,
Affiliation = #Affiliation;
How can I change my code to make it work?

MySQL Select INTO csv file separator working incorrectly

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 ,

When I use OPTIONALLY ENCLOSED BY '"' It still shows the double quotes

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

escape sql keywords a csv file when inserting in mysql

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';

How to SELECT INTO OUTFILE with header labels enclosed by quotes but not body fields?

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