Importing data from csv file - oracle

I'm trying to import some data (using oracle sql developer) from a .csv file but I'm getting an error as :-
Verifying if the Date columns have date formats FAILED Date columns ... column names
the date in my .csv file is :
2008-01-09 15:59:23.187
I have tried giving this format but it doesn't work (in the data importing wizard)
yyyy-mm-dd HH24:mi:ss.ff3
I'm trying to figure out a solution expecting some help.
Thanks.

I can't test right now but I'm taking a guess: the format is not a DATE format but a TIMESTAMP format. Consider:
SQL> select to_date('2008-01-09 15:59:23.187',
2 'yyyy-mm-dd HH24:mi:ss.ff3') from dual;
ORA-01821: date format not recognized
SQL> select to_timestamp('2008-01-09 15:59:23.187',
2 'yyyy-mm-dd HH24:mi:ss.ff3') ts from dual;
TS
-------------------------------------------------
09/01/08 15:59:23,187000000
If this is the same error that SQL Dev encounters, you could import in a timestamp column.

Oracle Date's can only store to seconds, you will need to use the timestamp.
Using a timestamp works fine (date fails with error of "Fractional seconds format element not allowed in DATE formatting")
create table test2(cola number(1), colB varchar2(5), colD timestamp);
csv file:
colA, colB, colD
"1","a","2008-01-09 :15:59:23.187"
"2","b","2009-02-10 :16:48:32.188"
"3","c","2012-03-11 :17:37:41.189"
"Import Data" in SQL Developer 3.0.03 using colD format of yyyy-mm-dd HH24:mi:ss.ff3
select * from test2;
COLA COLB COLD
---------------------- ----- -------------------------
1 a 09-JAN-08 03.59.23.187000000 PM
2 b 10-FEB-09 04.48.32.188000000 PM
3 c 11-MAR-12 05.37.41.189000000 PM

Related

Web logs timestamp import to Oracle SQL

I have some web logs that I'm trying to import into Oracle SQL table.
The table has timestamp and varchar for when request was made and url.
I formatted the logs to looks like this:
"Nov 1 2021 2:12:54.856 CDT","/webfolder1/file.html"
"Dec 11 2021 5:32:13.34 CDT","/webfolder1/file2.html"
I used SQL developer tool to import this file to Oracle table, however it did not like the date in the first column.
VALUES (to_timestamp('Nov 1 2021 2:12:54.856 CDT'), '/webfolder1/file.html')
...
ERROR:
ORA-01858: a non-numeric character was found where a numeric was expected
Do I need to use sed/awk or some other utility to change the date into some other format that Oracle would accept?
Use TO_TIMESTAMP_TZ and specify a format model and language:
CREATE TABLE table_name (
ts TIMESTAMP WITH TIME ZONE,
filename VARCHAR2(50)
);
Then:
INSERT INTO table_name (ts, filename) VALUES (
to_timestamp_tz(
'Nov 1 2021 2:12:54.856 CDT',
'Mon DD YYYY HH24:MI:SS.FF3 TZD',
'NLS_DATE_LANGUAGE=English'
),
'/webfolder1/file.html'
);
db<>fiddle here

Convert timezone format in Oracle

I need to convert below timezone format in the following format:
Input:
2020-10-28T20:12:20.986Z
Output:
28-OCT-20 8:12 PM
I tried below query but I am unable to get timestamp with it. Please help.
select TO_TIMESTAMP(SUBSTR('2020-04-21T13:02:31.259Z',1,(INSTR('2020-04-21T13:02:31.259Z', 'T') - 1)),'YYYY-MM-DD HH24:MI:SS') from dual;
One option might be this
SQL> alter session set nls_timestamp_format = 'dd-MON-YY hh:mi PM' ;
Session altered.
SQL> select to_timestamp('2020-10-28T20:12:20.986Z','yyyy-mm-dd"T"hh24:mi:ss.ff3"Z"') from dual ;
TO_TIMESTAMP('2020-10-28T20:12:20.986Z','YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
---------------------------------------------------------------------------
28-OCT-20 08:12 PM
SQL>
But if you rely better in the to_timestamp function without any session setting, then it is better
SQL> select to_timestamp('2020-10-28T20:12:20.986Z','yyyy-mm-dd"T"hh24:mi:ss.ff3"Z"') from dual ;
TO_TIMESTAMP('2020-10-28T20:12:20.986Z','YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
---------------------------------------------------------------------------
28-OCT-20 08.12.20.986000000 PM
You have a timestamp string with a time zone, use TO_TIMESTAMP_TZ rather than TO_TIMESTAMP and then use TO_CHAR to format it:
SELECT TO_CHAR(
TO_TIMESTAMP_TZ(
'2020-04-21T13:02:31.259Z',
'YYYY-MM-DD"T"HH24:MI:SS.FFTZR'
),
'DD-MON-RR HH12:MI AM',
'NLS_DATE_LANGUAGE=American'
)
FROM DUAL;
db<>fiddle here
Note: DATE, TIMESTAMP and TIMESTAMP WITH TIME ZONE are binary data types and are stored in 7-20 bytes (1 byte each for century, year-of-century, month, day, hour, minute and second then up to 6 optional bytes for fractional seconds for TIMESTAMPs and up to 7-bytes for time zone for TIMESTAMP WITH TIME ZONE). It is never stored in any particular format.
How the DATE/TIMESTAMP data types are displayed is dependent on the client application that you are using to query the database; some may use the NLS settings for the user's session but others do not use that. If you want a particular format then convert the DATE/TIMESTAMP to a string using TO_CHAR.

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.

Default milliseconds to 0 in Oracle

I have a timestamp column in Oracle that has format 'MM/DD/YYYY HH24:MI.SxFF6'.
The data looks like below:
11/09/1917 10:45:28.230000
10/19/2014 18:09:28.410000
12/19/2011 11:06:28.340000
I need the timestamp to retain the value except for getting the milliseconds which need to be defaulted to 000000.
I tried query -
cast(to_char(Local_time, 'MM/DD/YYYY HH24:MI:SS') as timestamp(6))
But it is throwing error - "Not valid month"
Does anyone have any ideas on what I can try to get milliseconds to 0. I use Toad to query the table.
Your TIMESTAMP value does not have any format. All you have is a default display format - defined by current user NLS_TIMESTAMP_FORMAT setting.
Try this one:
CAST(Local_time AS TIMESTAMP(0))
If you like to trunc the milliseconds but haven them still available use
CAST(CAST(Local_time AS TIMESTAMP(0)) AS TIMESTAMP(6))
Something like this, perhaps?
SQL> create table test (col timestamp, result timestamp);
Table created.
SQL> insert into test (col) values (to_timestamp('11/09/1917 15:45:28.230000', 'MM/DD/YYYY HH24:MI:SS.FF6'));
1 row created.
SQL> update test set result = cast(col as date);
1 row updated.
SQL> select * From test;
COL RESULT
------------------------- -------------------------
09.11.17 15:45:28,230000 09.11.17 15:45:28,000000
SQL>

Oracle virtual column, date and timestamp

I have a problem in Oracle, with a virtual column (I need the timestamp starting from a date column); here my example:
CREATE TABLE TBDATETIME(
DATETIME_1 DATE,
DATETIME_2 TIMESTAMP(9) GENERATED ALWAYS AS (
CAST( TO_CHAR(DATETIME_1, 'DD/MM/YYYY HH24:MI:SS')
|| '.' || TO_CHAR(DATETIME_1, 'FF9') AS TIMESTAMP(9))
) VIRTUAL
);
INSERT INTO TBDATETIME(DATETIME_1)
VALUES(SYSDATE);
COMMIT;
SELECT *
FROM TBDATETIME;
ORA-01821: date format not recognized
01821. 00000 - "date format not recognized"
*Cause:
*Action:
Where is the problem fetching data? I tried a lot of format masks, but nothing helps...
DATETIME_1 DATE
TO_CHAR(DATETIME_1, 'FF9')
That's the problem - DATE doesn't have fraction seconds
And it's dangerous to cast char to timestamp (the result depends on nls settings)
I suppose you want this:
CREATE TABLE TBDATETIME(DATETIME_1 DATE, DATETIME_2 TIMESTAMP(9) GENERATED ALWAYS AS (CAST(DATETIME_1 AS TIMESTAMP(9))) VIRTUAL);

Resources