Setting the Date Format in the SQL*Loader Control File - oracle

I have csv file that has "17 September, 2009 11:06:06 AM" as COMPLETED_ON variable
I am using sql loader to load data to oracle with folowing:
LOAD DATA
INFILE 'c:/load/file_name.csv'
APPEND
INTO TABLE tbl_name
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(
COMPLETED_ON DATE "not sure what to put here",
)
in oracle I have this column created as follows:
"COMPLETED_ON" TIMESTAMP (0) WITH LOCAL TIME ZONE
How do I change COMPLETED_ON date in control file?

I'm not 100% with the LOAD DATA INFILE syntax, but I know the format string 'DD Month, YYYY HH:MI:SS AM' matches your date format. I was able to use it in a TO_DATE to convert your sample date. Try this:
LOAD DATA
INFILE 'c:/load/CW_COMPLIANCE.csv'
APPEND
INTO TABLE tbl_name
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
(
COMPLETED_ON DATE 'DD Month, YYYY HH:MI:SS AM',
)

Related

Oracle SQL loader control file creation for date and time

I am using below CTL file to load data into table
Load data
Append
Into table abc
Fields terminated by ',' optionally enclosed by '"'
Trailing nullcols
(
R_date date 'mm/dd/yyyy hh:mm:ss'
)
Csv file value is as
R_date
09/12/2023 12:30:34
08/11/2023 22;30:45
In table abc r_date column datatype is date.
Ora-01840 input value not long enough for date format.
Noting we have written in above file
I think you want:
R_date date "mm/dd/yyyy hh24:mi:ss"

Custom date format in Oracle

I have dates in VARCHAR column in a table with this format.
"2019-08-13 00:00:00"
And few are in this format
"Wed Feb 20 2019 15:00:58 GMT+0100"
I want all my fields to be in (2).
I cannot have date type for this column. Please help me in converting
You say that this column can't be a date. But if you are storing dates (or, in this case, timestamps with time zones), you really, really want that column to be the proper data type. Otherwise, you're going to be setting yourself up for a world of pain when someone inserts a string in the wrong format.
Assuming all strings of length 19 are in the first format, they all represent valid dates, and you want all of them to end up in the GMT+1 time zone, something like
select case when length(column_name) = 19
then to_char( to_date( column_name, 'yyyy-mm-dd hh24:mi:ss' ),
'Dy Mon DD YYYY HH24:MI:SS' ) || ' GMT+0100'
else column_name
end
from your_table

Oracle convert hhmmss to hh:mm:ss

I have some timestamp data in an old Oracle database that needs converting into HH:MM:SS. After trying to use to_char function, the value I give is not readable (E.g. 105001, to_char('105001','HH24:MI:SS)), this SQL will break. I can convert sysdate into the incorrect format but I can't reverse the procedure.
For example:
select to_char(sysdate, 'HHmiss')from table
returns '105001'
I need something that will convert the hhmmss format into HH:MM:SS so when I produce a select statement it is in a readable format.
You can first select from dual table which is virtual table
There are 2 different way to have time
24 hours : like 5 and 15
select to_char(sysdate, 'HH24:MI:SS')from dual
Result
14:25:56
12 hours : like 2 AM and 2 PM
select to_char(sysdate, 'HH:MI:SS AM')from dual
Result
02:22:35 PM
Assuming that your values are a NUMBER in the database which is six-digits long and represents an HHMMSS value you can format it as you want by using SUBSTR:
SELECT SUBSTR(TO_CHAR(SOME_TIMESTAMP, 'FM000000'), 1, 2) || ':' ||
SUBSTR(TO_CHAR(SOME_TIMESTAMP, 'FM000000'), 3, 2) || ':' ||
SUBSTR(TO_CHAR(SOME_TIMESTAMP, 'FM000000'), 5, 2)
FROM cteNumbers
db<>fiddle here
I have some timestamp data in an old Oracle database that needs converting into HH:MM:SS
Just use HH24 to get a 24-hour clock and add the : separators to your format model and then apply that format directly to your TIMESTAMP column using the TO_CHAR function:
Oracle 11g R2 Schema Setup:
CREATE TABLE table_name ( your_timestamp_column TIMESTAMP );
INSERT INTO table_name ( your_timestamp_column )
VALUES ( TIMESTAMP '2018-09-24 12:34:56' );
Query 1:
SELECT TO_CHAR( your_timestamp_column, 'HH24:MI:SS') FROM table_name
Results:
| TO_CHAR(YOUR_TIMESTAMP_COLUMN,'HH24:MI:SS') |
|---------------------------------------------|
| 12:34:56 |
You do not need to output it as a HHMMSS string and then try to reformat it to add separators as that is needlessly complicated.

DateTime Format in Oracle SQL Loader [duplicate]

The data from the infile is in the format MM/DD/YYYY how do I tell the control file to load it into the database as YYYYMM?
When you specify the columns in the INFILE declare just identify the format the data is held in. Like this
load data
infile 'whatever.csv'
into table t23
fields terminated by ','
trailing nullcols
(
col_1 integer
, col_2 char
, col_3 date "MM/DD/YYYY"
, col_4 date "MM/DD/YYYY"
, col_5 char
)
Don't worry about the "to" date format. That is only for display purposes. Oracle stores dates in its own internal representation.
Are you trying to load the MM/DD/YYYY data into a char/varchar2 field, or into a date field?
If you're trying to load it into a date field and you want to preserve the day of the month, APC's answer is correct. You can always just present the YYYYMM if that's what you want to do.
If you're trying to load it into a date field and you want to truncate it to the first day of the month, I think something like this would work:
date_column date "MM/DD/YYYY" "trunc(:date_column, 'mm')"
If inserting into a CHAR/VARCHAR2 column, you'd could to transform it a little differently:
vc2_column char "substr(:vc2_column, 7, 4) || substr(:vc2_column, 1, 2)"

SQLLDR : merger year,mon and day

I have csv file that contains shown below :
year,mon,day
2014,12,01
2013,10,30
and abc's table
create table abc (
year varchar2(4),
mon varchar2(2),
day varchar2(2),
date1 date
)
how to tell loader.ctl to merger year,mon,day become date?
like that ?
OPTIONS (SKIP=11, errors=12000)
LOAD DATA
APPEND INTO TABLE abc (
year "trim (:year)",
mon "trim (:mon)",
day "trim (:day)",
date1 "to_date (year||mon||day,'yyyymmdd')"
)
Since your fields are comma separated, you should include fields terminated by "," in your control file.
date1 is not present in the data file, but is computed. So use EXPRESSION parameter to specify that.
in to_date function, use colons to reference the previous fields.
So, your control file should look like this,
OPTIONS (SKIP=11, errors=12000)
LOAD DATA
APPEND INTO TABLE abc
fields terminated by ","
(
year "trim (:year)",
mon "trim (:mon)",
day "trim (:day)",
date1 EXPRESSION "to_date (:year||:mon||:day,'yyyymmdd')"
)

Resources