I have a requirement to load the "dates" from a text file. I have tried like this
OPTIONS(skip=1)
load data
infile 'mypath\sample.txt'
APPEND
into table sample
fields terminated by "|"
TRAILING NULLCOLS
(
name CHAR(500) NULLIF (name=BLANKS)
, update_date date "MM/DD/RRRR HH:MI:SS" NULLIF (update_date=BLANKS)
)
My system date format is "6/21/2000 8:29:58 AM"
It is not showing any error in the BAD file. but 64 records were there in the bad file. please help me.
thanks
It would be better if you posted a few sample input lines; anyway, using the following example, it works fine in my case. Have a look, compare it with your attempt, make changes (if needed).
First, a table:
SQL> create table sample
2 (name varchar2(30),
3 update_date date);
Table created.
SQL>
Control file (which includes sample data):
options (skip=1)
load data
infile *
replace
into table sample
fields terminated by '|' trailing nullcols
(
name,
update_date "to_date(:update_date, 'mm/dd/yyyy hh:mi:ss am')"
)
begindata
name | update_date
little | 6/21/2000 8:29:58 AM
foot | 12/21/2007 10:15:12 PM
Loading session:
M:\a1_maknuto>sqlldr little/foot#orcl control=test19.ctl log=test19.log
SQL*Loader: Release 11.2.0.2.0 - Production on Pon Sij 8 07:17:52 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 1
Commit point reached - logical record count 2
M:\a1_maknuto>
Result:
SQL> select * from sample;
NAME UPDATE_DATE
------------------------------ -------------------
little 21.06.2000 08:29:58
foot 21.12.2007 22:15:12
SQL>
[EDIT: how to subtract 12 hours]
SQL> alter session set nls_date_format = 'mm/dd/yyyy hh:mi:ss am';
Session altered.
SQL> with test as
2 (select to_date('12/19/2017 12:49:48 PM', 'mm/dd/yyyy hh:mi:ss am') col
3 from dual
4 )
5 select
6 col,
7 col - 12/24 sub_12_hours
8 from test;
COL SUB_12_HOURS
---------------------- ----------------------
12/19/2017 12:49:48 PM 12/19/2017 12:49:48 AM
SQL>
Related
Hi everyone I wanna ask u about how I can bring data last 24 hours into bar charts, is there any methods to make it please
I have this table without data
datetime
clientchannel
servicename
service_count
13_02_2022 9:35
*****
notification
2
It is a WHERE clause you need, I presume. Something like this:
select ...
from your_table
where datetime >= sysdate - 1;
Why? Because - when you subtract a number from DATE datatype value in Oracle - it subtracts that many days.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate right_now,
2 sysdate - 1 yesterday
3 from dual;
RIGHT_NOW YESTERDAY
------------------- -------------------
13.02.2022 11:01:34 12.02.2022 11:01:34
SQL>
If you store date values as strings (which means that DATETIME column is declared as e.g. VARCHAR2(20), and that's really bad idea), then you first have to convert it to a valid date datatype value - use TO_DATE function with appropriate format mask:
where to_date(datetime, 'dd_mm_yyyy hh24:mi') >= sysdate - 1
[EDIT] If you want to go 60 minutes back, then subtract that many minutes:
SQL> select sysdate right_now,
2 sysdate - interval '60' minute an_hour_ago
3 from dual;
RIGHT_NOW AN_HOUR_AGO
------------------- -------------------
14.02.2022 07:09:30 14.02.2022 06:09:30
SQL>
I have a control.ctl file and I'm using SQL*LOADER to load this data in the table.
LOAD DATA
INFILE 'data_for_insert.csv'
INSERT INTO TABLE TABLE_NAME
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
COL1,
COL2,
NEXT_MONDAY EXPRESSION "SELECT NEXT_DAY(SYSDATE, 'MONDAY') FROM dual",
TODAY SYSDATE
)
Error received:
Record 16: Rejected - Error on table TABLE_NAME, column NEXT_MONDAY .
ORA-00936: missing expression
I can't find what is the problem because the expression SELECT NEXT_DAY(SYSDATE, 'MONDAY') FROM dual works when I run it in SQL Developer.
If I take that expression out it works so there's no problems in the rest of the code. Can someone help? thanks!
If that's an expression, then use an expression, not the whole query; not that it won't work (you found out that it actually works), it's just unnecessary.
Control file:
load data
infile *
replace
into table test
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
(col1,
col2,
next_monday expression "next_day (sysdate, 'MONDAY')",
today sysdate
)
begindata
1,2
3,4
Testing:
SQL> $sqlldr scott/tiger control=test9.ctl log=test9.log
SQL*Loader: Release 11.2.0.2.0 - Production on Pet Stu 26 21:24:37 2021
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 1
Commit point reached - logical record count 2
SQL> select * from test;
COL1 COL2 NEXT_MONDA TODAY
---------- ---------- ---------- ----------
1 2 29.11.2021 26.11.2021
3 4 29.11.2021 26.11.2021
SQL>
In the end this solution seems to have solved the problem. I added parentheses inside the double quotes.
NEXT_MONDAY EXPRESSION "(SELECT NEXT_DAY(SYSDATE, 'MONDAY') FROM dual)"
I have a table with TIMESTAMP datatype and I need to compare month value to select all table values. Example the created is the field and wanted to get all the rows which is created between November & December of any year. Tried with below query and it don't work.
select * from table_name where TO_CHAR(created_time, 'mon') in ('nov','dec')
Use EXTRACT:
SELECT *
FROM table_name
WHERE EXTRACT( MONTH from created_time ) IN ( 11, 12 )
Or, you can use TO_CHAR( created_time, 'MM' ) to get the numeric month value (and not worry about language settings as you would have to with the MON format model):
SELECT *
FROM table_name
WHERE TO_CHAR( created_time, 'MM' ) IN ( '11', '12' )
db<>fiddle
Well, there are several ways to achieve this.
1.Using the nlsparam of to_char function
The 'nlsparam' argument specifies the language in which month and day names and abbreviations are returned.
Example
SQL> create table t ( c1 timestamp ) ;
Table created.
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss.rr' ;
Session altered.
SQL> insert into t values ( systimestamp - 30 ) ;
1 row created.
SQL> insert into t values ( systimestamp ) ;
1 row created.
SQL> select * from t ;
C1
---------------------------------------------------------------------------
30.07.2020 09:29:35.20
29.08.2020 09:29:42.20
SQL> select value from nls_database_parameters where parameter='NLS_LANGUAGE' ;
VALUE
--------------------------------------------------------------------------------
AMERICAN
SQL> select c1 , to_char(c1, 'mon' , 'NLS_DATE_LANGUAGE = american') as mon from t ;
C1
---------------------------------------------------------------------------
MON
------------
30.07.2020 09:29:35
jul
29.08.2020 09:29:42
aug
2.However, to avoid depending in the language, you can use extract and compare the number of the month which is the same in any language. In this case you need to convert the timestamp to a date, but before you need to set the nls_timestamp_format to the specific format.
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select c1 , extract(month from to_date(c1,'dd.mm.yyyy hh24:mi:ss')) from t ;
C1
---------------------------------------------------------------------------
EXTRACT(MONTHFROMTO_DATE(C1,'DD.MM.YYYYHH24:MI:SS'))
----------------------------------------------------
30.07.2020 09:29:35
7
29.08.2020 09:29:42
8
load data
infile 'c:\oracle_toad\sql_loader\v1_data.txt'
replace into table v1 fields terminated by ','
( a integer external, b char, c char )
1,2,"da,ta1"
2,4,"dat,a2"
2,4,"da,ta2"
"" are not supposed to be inserted as a part of data. That's just for reference.
I intentionally inserted "," in each of the data set.
I am hoping to insert 1, 2, "da,ta1" <<< like this. Is there a way that I can include the separator "," within the data set?
Here's an example:
Test table:
SQL> create table test (col1 number, col2 varchar2(20), col3 varchar2(20));
Table created.
Control file:
load data
infile *
replace
into table test
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
(
col1,
col2,
col3
)
begindata
1,2,"da,ta1"
2,4,"dat,a2"
2,4,"da,ta2"
Loading session & the result:
SQL> $sqlldr scott/tiger control=test04.ctl log=test04.log
SQL*Loader: Release 11.2.0.2.0 - Production on Pon Kol 27 14:11:26 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 2
Commit point reached - logical record count 3
SQL> select * From test;
COL1 COL2 COL3
---------- -------------------- --------------------
1 2 da,ta1
2 4 dat,a2
2 4 da,ta2
SQL>
I have one temp table which is empty now. I want to load the data from that flat file to the oracle temp table. In one column col3 of the flat file mention as "X" but in the table i want to insert as "abc". If possible to remove column value from "X" in flat file then how it is possible? or replace value from "X" to "abc".
SQL*Loader lets you apply SQL operators to fields, so you can manipulate the value from the file.
Let's say you have a simple table like:
create table your_table(col1 number, col2 number, col3 varchar2(3));
and a data file like:
1,42,xyz
2,42,
3,42,X
then you could make your control file replace an 'X' value in col3 with the fixed value 'abc' using a case expression:
load data
replace
into table your_table
fields terminated by ',' optionally enclosed by '"'
trailing nullcols
(
col1,
col2,
col3 "CASE WHEN :COL3 = 'X' THEN 'abc' ELSE :COL3 END"
)
Running that file through with that control file inserts three rows:
select * from your_table;
COL1 COL2 COL
---------- ---------- ---
1 42 xyz
2 42
3 42 abc
The 'X' has been replaced, the other values are retained.
If you want to 'remove' the value, rather than replacing it, you could do the same thing but with null as the fixed value:
col3 "CASE WHEN :COL3 = 'X' THEN NULL ELSE :COL3 END"
or you could use nullif or defaultif:
col3 nullif(col3 = 'X')
DECODE, right?
SQL> create table test (id number, col3 varchar2(20));
Table created.
SQL> $type test25.ctl
load data
infile *
replace into table test
fields terminated by ',' trailing nullcols
(
id,
col3 "decode(:col3, 'x', 'abc', :col3)"
)
begindata
1,xxx
2,yyy
3,x
4,123
SQL>
SQL> $sqlldr scott/tiger#orcl control=test25.ctl log=test25.log
SQL*Loader: Release 11.2.0.2.0 - Production on ╚et O×u 29 12:57:56 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 3
Commit point reached - logical record count 4
SQL> select * From test order by id;
ID COL3
---------- --------------------
1 xxx
2 yyy
3 abc
4 123
SQL>