Oracle External Table - How to convert field to a date? - oracle

I'm loading some data into oracle using an external table. One of the fields "CREATEDON" is a date but is formatted like this 20151231
How can I convert CREATEDON to a proper oracle date datatype. This is my attempt so far which doesn't work...
CREATE TABLE "AMS"."DATA"
( "BLANK" VARCHAR2(255 BYTE),
"BLANK2" VARCHAR2(255 BYTE),
"CLIENT" VARCHAR2(255 BYTE),
"MATERIAL" DATE,
"CREATEDON" VARCHAR2(255 BYTE)
)
ORGANIZATION EXTERNAL
( TYPE ORACLE_LOADER
DEFAULT DIRECTORY "DIR"
ACCESS PARAMETERS
( records delimited BY '\r\n'
skip 1
fields terminated BY '|' lrtrim missing field VALUES are NULL
)
LOCATION ( "DIR":'foo.txt' )
)
REJECT LIMIT UNLIMITED ;

You have two options:
Keep as is, and transform in your select statements, such as:
select ..., to_date(CREATEDON,'YYYYMMDD') CREATEDON
Define the format in the table DDL:
"CREATEDON" VARCHAR2(255 BYTE) DATE_FORMAT DATE MASK "YYYYMMDD"

Related

Char and Varchar2 Columns showing Larger size in View

I have a table like:
Table
{
...
...
SETTLEMENTDAY VARCHAR2(10 CHAR)
ACCOUNT VARCHAR2(50 CHAR)
AMOUNT NUMBER(38,5)
CURRENCY VARCHAR2(3 CHAR)
...
...
}
When I am creating View using this table I am getting the following:
View
{
...
...
SETTLEMENTDAY VARCHAR2(40 CHAR)
ACCOUNT VARCHAR2(200 CHAR)
AMOUNT NUMBER(38,5)
CURRENCY VARCHAR2(12 CHAR)
...
...
}
There is no casting.
I am using Oracle 12.2.0.1.0.
I tried to use following alter query but not helping
alter session set nls_length_semantics=CHAR;
I am not able to understand why the size is getting multiplied by 4. Which variable in database could have affected that.
Thanks,

External Tables - Replace comma with dot in numbers

How can I replace a comma with a dot directly from the external table?
I have a CSV with that format:
aaa;12345.67;bbbbbb
ccc;23132;eeeee
Sometimes someone puts a line like that:
ddd;1111,22;fff
CREATE TABLE MYTAB_EXT
(
"TX1" VARCHAR2(20 BYTE),
"VAL1" NUMBER(13,3),
"TX2" VARCHAR2(20)
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER DEFAULT DIRECTORY "EXT_TABLES_FOO" ACCESS
PARAMETERS (
records delimited BY newline
SKIP 1
fields terminated BY ';' LRTRIM
missing field VALUES are NULL (
TX1 ,
VAL1 ,
TX2)
) LOCATION ( 'MYTAB.csv' )
)
REJECT LIMIT UNLIMITED;
Thanks

sql*loader loading csv file with 3.5 million records

How can i load csv file with 3.5 million records into database table?
my csv file format:
code,doi,type,j_code,title,is_no,ib_no
"ETO072","10.10ISSN","J_ID","BEJ","ABCDEF","19-38","07-93"
"ETO073","10.11ISSN","J_ID","BEJ","NSKDJJ","19-39","07-94"
"ETO074","10.12ISSN","J_ID","BEJ","WEWDSD","19-40","07-95"
"ETO075","10.13ISSN","J_ID","BEJ","UOISDJ","19-41","07-96"
table structure:
CREATE TABLE CNTNT
( ID NUMBER,
code NUMBER,
TYPE VARCHAR2(255 BYTE),
is_no VARCHAR2(13 BYTE),
ib_no VARCHAR2(13 BYTE),
J_code VARCHAR2(255 BYTE),
TITLE VARCHAR2(1000 BYTE),
JCODE VARCHAR2(20 BYTE),
DOI VARCHAR2(255 BYTE),
PRIMARY KEY (ID),
);
control file:
load data
infile 'test.csv' "str '\n'"
append
into table CNTNT
fields terminated by ','
OPTIONALLY ENCLOSED BY '"' AND '"'
trailing nullcols
(
code,
DOI ,
TYPE,
J_CODE ,
TITLE ,
is_no,
ib_no,
ID "id_seq.nextval"
)
this is what i am using to run loader:
sqlldr CONTROL=test.ctl
LOG=test.log
BAD=test.bad
skip=1;
What are the changes i need to do in control file or sqlldr command to achieve this?

load decimal into oracle table with sql loader

I have this table in oracle :
CREATE TABLE mytable
(
TSTAMP Date,
prmc1 VARCHAR2(30),
prmc2 VARCHAR2(30),
prmc3 VARCHAR2(30),
prmc4 VARCHAR2(30),
prmc5 NUMBER,
prmc5 NUMBER,
prmc6 NUMBER
)
the control file is below :
load data
append
into table mytable
fields terminated by ',' TRAILING NULLCOLS
( tstamp DATE "YYYY-MM-DD HH24:MI" TERMINATED BY ",",
prmc1 ":prmc1",
prmc2 ":prmc2",
prmc3 ":prmc3",
prmc4 ":prmc4",
prmc5 INTEGER ":prmc5",
prmc6 INTEGER ":prmc6"
)
the value of the column prmc5 in the csv file is -106.436
how do i load this into the table?
You just need to specify it as DECIMAL EXTERNAL
I am also removing specifying the format as the column itself.
load data
append
into table mytable
fields terminated by ',' TRAILING NULLCOLS
( tstamp DATE "YYYY-MM-DD HH24:MI" TERMINATED BY ",",
prmc1 ,
prmc2 ,
prmc3 ,
prmc4 ,
prmc5 DECIMAL EXTERNAL,
prmc6 DECIMAL EXTERNAL
)
More details with some Example here

Invalid cast from Int32 to Decimal

I am using ODP.NET with EF in an MVC4 application. When performing the below operation,
_db.Set<User>().FirstOrDefault(x => x.Email == Email || x.DisplayName == DisplayName)
it throws exception
[Specified cast is not valid.] Oracle.DataAccess.Client.OracleDataReader.GetDecimal(Int32 i)
What I don't understand is that my User Object has no Int fields, I made them all decimal to smooth things over with Oracle DB.
My user table:
CREATE TABLE USERS (
USER_ID NUMBER(38) NOT NULL,
DISPLAY_NAME VARCHAR2(50 CHAR),
IS_LOCKED_OUT_FL VARCHAR2(1 CHAR),
IS_LOCKED_REASON VARCHAR2(256 CHAR),
IS_DELETED_FL VARCHAR2(1 CHAR),
EMAIL VARCHAR2(254 CHAR),
TITLE VARCHAR2(20 CHAR),
FIRSTNAME VARCHAR2(150 CHAR),
SURNAME VARCHAR2(150 CHAR),
DATECREATED DATE DEFAULT SYSDATE,
SCHEME_ID NUMBER(38),
STATUS VARCHAR2(20 CHAR) DEFAULT 'New',
PHONE VARCHAR2(20 CHAR) DEFAULT '',
PRODUCT_ID NUMBER(38) DEFAULT 0,
CONSTRAINT BCARD_USERS_PK PRIMARY KEY (USER_ID),
CONSTRAINT UK_BCARD_USERS UNIQUE (SCHEME_ID))
TABLESPACE SYSTEM
STORAGE (
INITIAL 64K
NEXT 1M
MAXEXTENTS UNLIMITED
)
I don't even know where to start debugging because the error is caused by ODP.NET and I can't step into the library. I should also mention that this doesn't happen on my 32-bit development machine, but the same code throws the exception on a 64-bit production machine
using the same 32-bit library. My project was built the target AnyCPU.
To be clear, the Int32 parameter of the GetDecimal method is the index of the column - it's throwing an error trying to convert column with index 'i'. It's not trying to convert anything to an int.
That said, my guess would be that your User object has a decimal field that's not nullable (decimal?), yet some of your columns like scheme_id and product_id allow nulls in the database.

Resources