Web logs timestamp import to Oracle SQL - oracle

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

Related

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.

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.

What must be considered when modifying Oracle DATE column to TIMESTAMP

The system I'm working on uses an oracle 11 db and has a java implemented backend which uses JDBC.
The system has one main performance problem:
The oracle db has indexed DATE columns but the java backend access the db like this
String sql = "SELECT foo FROM bar WHERE a_date > ?";
// ... prepare the statement
statement.setTimestamp(1, new Timestamp());
then Oracle will not use the index and fall back to full table scan because of statement.setTimestamp.
First Solution:
So I have changed the access of the backend from statement.setTimestamp to statement.setDate.
The performance has been improved and the DATE index was now used, but now I lost the accuracy of the timestamp data type, and only got a day-accuracy of the java.sql.Date type.
Thus, the first solution was unusable for my system.
Second Solution:
I don’t change the java backend thus using statement.setTimestamp and change all DATE columns of the oracle db like the following sql command example:
ALTER TABLE <table_1> MODIFY <date_column> TIMESTAMP;
Now my question:
What must be considered when modifying Oracle DATE column to TIMESTAMP
I think a list of the important aspects would be perfect.
Here my first draft of the "CHECKLIST of Migrating Oracle DATE columns to TIMESTAMP
1. CREATE / INSERT
2. READ / SELECT
3. UPDATE / UPDATE
4. DELETE / DELETE
5. TO_DATE( )
select * from table where crea_time between TO_DATE( '10.2014', 'MM.yyyy' ) and TO_DATE( '12.2014', 'MM.yyyy' ) ;
6. TRUNC( )
select TRUNC( crea_time ) from table;
output of crea_time data typ DATE
05.11.2014 00:00:00
output of crea_time data typ TIMESTAMP
05.11.2014 00:00:00
7. TO_CHAR( )
select to_char( crea_time,'hh24miss' ) from table;
output data typ DATE
140612
output data typ TIMESTAMP
140612
8. NESTED - TRUNC( TO_DATE() )
select TRUNC(TO_DATE( crea_time), 'YEAR') from table;
output of crea_time data typ DATE
01.01.2014 00:00:00
output of crea_time data typ TIMESTAMP
ORA-01830:
01830. 00000 - "date format picture ends before converting entire input string"
*Cause:
*Action:
Solution >>> select TRUNC( TO_TIMESTAMP( crea_time), 'YEAR' ) from table;
output of crea_time data typ TIMESTAMP
01.01.2014 00:00:00
9. Add A DAY/HOUR/MINUTE/SECOND
select crea_time+1 from table;
output data typ DATE
06.11.2014 14:06:12
output data typ TIMESTAMP
06.11.2014 14:06:12
10. MIN() / MAX()
SELECT MIN(crea_time) FROM table;
output data typ DATE
05.11.2014 14:06:12
output data typ TIMESTAMP
05.11.2014 14:06:12,000000000
Please excuse my bad English.

Importing data from csv file

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

Resources