Multiple enclosed-by symbols in SQL*Loader - oracle

I am loading a CSV file into an Oracle table. One field in some records is enclosed as "abc#xyz.com" and the same field in other records is enclosed as "'abc#xyz.com'". I need to load only abc#xyz.com.
I used OPTIONALLY ENCLOSED BY '"' but it does not help in the second case. Is there a way to specify two symbols in the OPTIONALLY ENCLOSED BY clause? Or what are the other ways of achieving this?

You can apply an SQL operator to trim the leading and trailing single quotes. For example, with a data file containing:
"abc#xyz.com"
"'abc#xyz.com'"
'abc#xyz.com'
And a control file for a dummy table:
LOAD DATA
TRUNCATE INTO TABLE t42
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
EMAIL CHAR(30) "TRIM(BOTH '''' FROM :EMAIL)"
)
This loads the stripped values:
select * from t42;
EMAIL
------------------------------
abc#xyz.com
abc#xyz.com
abc#xyz.com
As you can see, this will load values which are enclosed in single quotes, double quotes, or both - as long as the singles are within the doubles and not the other way around.

Related

External Table Help - Problem with FIELDS TERMINATED BY ',' if additional comma exists in a value in the field

I have a comma delimited txt file and I am using an external table to load the data down below:
create table test_ext_table
CUSTOMER_ID NUMBER,
CUSTOMER_NAME VARCHAR2(255),
CUSTOMER_NUMBER NUMBER)
ORGANIZATION EXTERNAL
( type oracle_loader
default directory TXT_DIR
access parameters
(RECORDS delimited by newline SKIP 1
FIELDS TERMINATED BY ','
LRTRIM
MISSING FIELD VALUES ARE NULL
)
LOCATION (TEST.txt)
)
REJECT LIMIT UNLIMITED);
I know the external table recognizes each field terminated by a comma, but let's say in the text file I have the following
TEST.txt
customer_id, customer_name, customer_number
1,a,10
2,b,11
3,c,12
4,Hello, Inc,13
For the 4 row in the txt file, because there is an additional ',' in the customer_name field, the external table isn't able to properly read the customer name into the table. Is there a way for me to adjust the external table so that it ignores additional ',' or any special characters?
As far as I know, there are 2 ways to do that:
optionally enclose string into e.g. double quotes
change delimiter from comma to something else, e.g. semi-colon
Otherwise, there's no way to leave it "as is" and make Oracle recognize which comma represents what.

SQL Loader: Double quotes inside double quotes

Hi I have problem loading data with double quotes inside double quotes. I always got rejected and save in bad file.
My sql scripts is like this.
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
ACCESS PARAMETERS (
records delimited by newline
STRING SIZES ARE IN CHARACTERS
logfile DIR_LOGS:'logs%p.log'
badfile DIR_LOGS:'bads%p.bad'
discardfile DIR_LOGS:'discarded%p.dsc'
fields terminated by ',' optionally enclosed by '"'
missing field values are NULL
)
My data is like this:
Name, Address, Company Name,
"Juan "Julio" Dela Cruz", "Block 5, lot6 Frobes Subd", "REGUS Corp "A"".
"Ferdinand Magellan", "Block 5, lot6, Frobes Subd", "REGUS Corp"
I want to retain the double quotes in the name and company.
Juan "Julio" Dela Cruz and REGUS Corp "A". What can you guys recommend?
There's nothing you can do, as far as SQL*Loader is concerned. If values are optionally enclosed into double quotes, then you can NOT have double quotes within those values.
If you aren't allowed to modify data, you have to find someone who can so that optionally enclosing character is changed to something else (i.e. not the double quote). Otherwise you won't be able to successfully load data.

How to retain double quotes in a column while loading a file using SQL Loader

I am trying to load a txt file with | (pipe) delimiter to an Oracle table via SQL loader utility. All the fields are enclosed with double quotes. But there are some text fields in the files that have additional double quotes in addition to the enclosed ones that needs to be retained. All the table columns are defined as VARCHAR. Here's the control parameters am using
OPTIONS (DIRECT=TRUE,SKIP=1)
LOAD DATA
CHARACTERSET UTF8
INFILE aaa.txt
APPEND INTO TABLE info_table
FIELDS TERMINATED BY "|"
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
This is my sample file
"1"|"High "Gold Tip" Tea, 600"
"2"|""10000 Beers, Wines & Spirits""
Table should be loaded with the below details
Record 1:
Column 1 - 1
Column 2 - High "Gold Tip" Tea, 600
Record 2:
Column 1 - 2
Column 2 - 10000 Beers, Wines & Spirits
Unfortunately, there's nothing much to be said.
File format is bad. You can't enclose values into characters that are used in those fields themselves. As data contain double quotes, you'll have to optionally enclose values into something else, not double quotes.
However, as you already split values with pipe characters, what do you need double quotes to optionally enclose those field values? Omit them from the file and you won't have any problem (of such kind, of course; who knows what might come next, but that's another story).

Delimeter files issues

I do have a flat file with not a fixed structure like
name,phone_num,Address
bob,8888,2nd main,5th floor,avenue road
Here the last column Address has the value 2nd main,5th floor,avenue road but since the same delimeter , is used for seperating columns also i am not getting any clue how to handle the same.
the structure of flat file may change from file to file.
How to handle such kind of flat files while importing using Informatica or SQL * Loader or UTL Files
I will not have any access to flat file just i should read the data from it but i can't edit the data in flat file.
Using SQLLoader
load data
append
into table schema.table
fields terminated by '~'
trailing nullcols
(
line BOUNDFILLER,
name "regexp_substr(:line, '^[^,]+')",
phone_num "regexp_substr(:line, '[^,]+', 1, 2)",
Address "regexp_replace(:line, '^.*?,.*?,')"
)
you need to change your source file to enclose the fields in an escape character eg:
name,phone_num,Address
bob,8888,^2nd main,5th floor,avenue road^
then in sql-loader you'd put:
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '^'
just pick a delimiter that doesn't normally appear in your data.
If you could get the source data enclosed within double quotes ( or any quotes for that matter) you can make use of 'Optional Quotes' option in Informatica while reading from Flat file

SQLLDR -- selective loading from pipe delimited txt

I am looking for a way to do selective loading using SQLLDR.
The source file is in "pipe delimited" format.
I know there is a way for this if the source is in a predefined position. It is explained here, by using WHEN & POSITION keywords.
What could I do if the source file is "pipe or tab" delimited?
I am not sure what you mean with "selective loading"?
But ify you are only asking how you can load a file where each column is delimited with a pipe, then use the option FIELDS TERMINATED BY '|' in the control file.
See the chapter "Variable Record Format" in the SQL*Loader manual for more details and examples:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14215/ldr_concepts.htm#sthref476
Depending on which version of the SQLLDR version you are using, you can use the keyword FILLER to skip a field from the file.
The below instruction will skip the second field in the file.
LOAD DATA
TRUNCATE INTO TABLE T1
FIELDS TERMINATED BY ','
( field1,
field2 FILLER,
field3
)
LOAD DATA
INFILE 'c:\myfile.txt'
TRUNCATE INTO TABLE T1
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS (field1, field2 , field3, field4)

Resources