oracle sqlldr time format - oracle

I'm using oracle sqlldr (for bulk load operations), but I can't convert this datetime format (first column):
File contents:
Jan 1 1900 11:36:56:000PM|968|409|198|33|30|45|19
Jan 1 1900 11:36:57:000PM|967|415|198|34|33|43|21
Jan 1 1900 11:36:59:000PM|966|427|197|34|33|40|19
Control file contents:
load data
infile '/home/bim/oraload/data/AERO.SONDAJ.samsun.txt'
append
into table AERO.SONDAJ
fields terminated by "|"
TRAILING NULLCOLS
(
refsaat date 'MON DD YYYY HH24:mi:ss', --not running
bsnsvy,
yuks,
sck,
nem,
isba,
rzgyon,
rzghiz
)

Try something like this. Inorder for this to work, the refsaat type should be a timestamp type and not DATE data type. Date Data type does not store beyond seconds.
refsaat TIMESTAMP 'Mon DD YYYY HH:mi:ss:ff3PM'

Related

Convert Mon yyyy to yyyy-mm-dd format in oracle

I am sending date (August 2022) from java as a string to oracle plsql and i want to change the format from August 2022 to 01-08-22 in plsql. how can i do it ?
below is a part of code:
PROCEDURE CHECK_DUMMY
(
IN_SL_NO IN SIGN.SL_NO%TYPE,
IN_MONTH IN SIGN.MONTH%TYPE
)
AS
V_MON DATE := TO_DATE(IN_MONTH , 'DD-MM-YYYY');
You want to use the format model Month YYYY and specify the language you are using:
CREATE PROCEDURE CHECK_DUMMY
(
IN_SL_NO IN SIGN.SL_NO%TYPE,
IN_MONTH IN SIGN.MONTH%TYPE
)
AS
V_MON DATE := TO_DATE(IN_MONTH , 'Month YYYY', 'NLS_DATE_LANGUAGE=English');
BEGIN
-- Do Something
DBMS_OUTPUT.PUT_LINE( v_mon );
END;
/
Then:
BEGIN
CHECK_DUMMY(1, 'August 2022');
END;
/
Outputs:
2022-08-01 00:00:00
Note: In Oracle, a DATE is a binary data type comprising of 7 bytes that represent century, year-of-century, month, day, hours, minutes and seconds and always has those 7 components and is never stored with any particular format. If you want to output 01-08-22 for display purposes then use TO_CHAR(v_mon, 'DD-MM-YY') to convert the date to a formatted string.
fiddle

Oracle Date Issue in Where Clause

I am unable to get the date column to respect the where clause. Regardless what I do, it does not filter on date. I have tried all combinations of to_char and to_date in vain.
HAVING TO_CHAR(PAYMASTR.CHECK_DATE,'MM/DD/YYYY') > '01/01/2021'
I have also tried the code below with all combinations of to_char and to_date.
HAVING PAYMASTR.CHECK_DATE >= TO_DATE('01-01-2021 12:00:00 AM',
'MM-DD-YYYY HH:MM:SS AM')
The check_date of of type DATE.
Result set:
|COMPANY|EMPLOYEE|PAY_SUM_GRP|PAY_GRADE RATE|WAGE_AMOUNT|NET_PAY_AMT|GROSS_PAY|CHECK_DATE|
|-------|--------|-----------|--------------|-----------|-----------|---------|----------|
|2|5|REG 09|21.98|175.84|1459.96|2263.19|1/19/2007 12:00:00 AM|
|2|5|REG 09|21.98|175.84|1663.93|2589.43|1/5/2007 12:00:00 AM|
If CHECK_DATE column's datatype is DATE (should be! If it is VARCHAR2, you're doing it wrong!), then
having check_date > date '2021-01-01'
i.e. compare date to date literal.
Second code you posted is almost OK:
HAVING PAYMASTR.CHECK_DATE >= TO_DATE('01-01-2021 12:00:00 AM', 'MM-DD-YYYY HH:MI:SS AM')
--
MI for minutes; MM is for month
I found this article on code project that did the trick for me. I was struggling really hard to get the query to respect the date parameter in the queru. Setting the session to NLS_DATE_FORMAT worked. Not sure what other implications it may have. Will have to talk to the DBA.
Code Project
It's all about how Oracle stores and works with date DATATYPE
The date has seven components
Century, year, month, day, hour, minute, seconds
and all these components take seven bytes of storage.
Whenever you fetch a Date column from a table, the date value is formatted in a more readable form and this format is set in the nls_date_format parameter.
I am assuming you are grouping by CHECK_DATE otherwise you need to add this date filter with the WHERE clause.
So first check the datatype of your column CHECK_DATE
If it is date then
HAVING CHECK_DATE >= TO_DATE('01-01-2021', 'MM-DD-YYYY')
You don't have to provide hours, minutes, and seconds if omitted hours are rounded to 12 AM or 00 if the 24-hour format is used;
Or if you want to have hours as well then you used MM instead of MI for minutes.
HAVING CHECK_DATE >= TO_DATE('01-01-2021 00:00:00', 'MM-DD-YYYY HH24:MI:SS')
And this does not make sense
HAVING TO_CHAR(PAYMASTR.CHECK_DATE,'MM/DD/YYYY') > '01/01/2021'
You want to compare dates not characters and to_char will provide you a character string that has no sense of comparing with another string '01/01/2021'.
So if you are not grouping by CHECK_DATE user filter condition with WHERE clause
or check the datatype of CHECK_DATE if it is not DATE change it to DATE.

Javascript Date conversion in Hive

I have a date column as a string data type in MMMM Do YYYY, HH:mm:ss.SSS
(December 16th 2019, 21:30:22.000) format.
I'm trying to convert this into a timestamp data type in hive but couldn't able to achieve it because this format is not available in unixtime.
Is there any way to convert this in hive?
This method will preserve millisecond precision. First extract only parts compatible with SimpleDateFormat pattern using regex, then convert to datetime, concat with milliseconds (milliseconds lost after unix_timestamp conversion) and convert to timestamp:
select timestamp(concat(from_unixtime(unix_timestamp(dt,'MMM dd yyyy HH:mm:ss.SSS')),'.',split(dt,'\\.')[1]))
from
(select regexp_replace('December 16th 2019, 21:30:22.001','([A-Za-z]+ \\d{1,2})[a-z]{0,2} (\\d{4}), (\\d{2}:\\d{2}:\\d{2}\\.\\d+)','$1 $2 $3') as dt --returns December 16 2019 21:30:22.001
) s;
OK
2019-12-16 21:30:22.001
Time taken: 0.09 seconds, Fetched: 1 row(s)
Try this
SELECT from_unixtime(unix_timestamp) as new_timestamp from data ...
That converts a unix timestamp into a YYYY-MM-DD HH:MM:SS format, then you can use the following functions to get the year, month, and day:
SELECT year(new_timestamp) as year, month(new_timestamp) as month, day(new_timestamp) as day

Hadoop Hive Date String to UTC Time SQL

I have a String column in my database which looks like
07/12/2019 04:17:08 PM
I use the function
cast(from_unixtime(unix_timestamp(myfield, 'MM/dd/yyyy hh:mm:ss'),'yyyy-MM-dd HH:mm:ss') as timestamp)as mytime
This gives me the result of
2019-07-12 04:17:08.0
I want the result to be in utc format and look something like
2019-07-12 16:17:08.
How can i change this to be in utc format?
Use aaa to parse the AM/PM in datetime. from_unixtime converts it to yyyy-MM-dd hh:mm:ss by default where the hour part is 24 hour format.
from_unixtime(unix_timestamp(myfield, 'MM/dd/yyyy hh:mm:ss aaa'))

Date conversion from regexp output data -- ORA-01841: (full) year must be between -4713 and +9999, and not be 0

I am trying to convert a value to the normal date format. But i am receiving the error ORA-01841: (full) year must be between -4713 and +9999, and not be 0 . I have researched the answers for a similar question in this website but that doesn't address my issue.
-- How the data looks like
select REPLACE(REGEXP_SUBSTR(data_detail, '([^|]*)([$|]|$)', 1, 5), '|', '') from land.LAND_DATA;
Output
2017-11-16T04:45:05.000Z
2017-11-16T04:46:04.000Z
When i am trying to convert the above mentioned output in to an appropriate date format i am receiving the error. What mistake am i doing here
select
TO_DATE(
TO_CHAR(
TO_TIMESTAMP(
REPLACE(REGEXP_SUBSTR(data_detail, '([^|]*)([$|]|$)', 1, 5), '|', ''),
'YYYY-MM-DD"T"HH24:MI:SS.ff3"Z"'),'MM/DD/YYYY HH24:MI:SS'),
'MM/DD/YYYY HH24:MI:SS')
from land.LAND_DATA;
These results are input data for the TO_TIMESTAMP function:
2017-11-16T04:45:05.000Z
2017-11-16T04:46:04.000Z
A year comes first, then a month and a day, and a time follows, which has milliseconds at the very end (after the comma).
This is a snippet from your code:
TO_TIMESTAMP( ....... , 'MM/DD/YYYY HH24:MI:SS' )
the second parameter is a date format model, these letters have the following meaning (in order):
MM - Month (01-12; January = 01).
/ - separator
DD - Day of month (1-31)
/ - separator
YYYY - 4-digit year; S prefixes BC dates with a minus sign.
- separator (space)
HH24 - Hour of day (0-23).
: - separator
MI - Minute (0-59).
: - separator
SS - Second (0-59).
The above format models doesn't match a format of input data, because a month comes first in this format, but your data starts with a year !!!
Use the following format instead:
'yyyy-mm-dd"T"hh24:mi:ss.ff"Z"'
You can test whether a format is fine or gives an error using a simple query like this:
select
to_timestamp( '2017-11-16T04:45:05.000Z', 'yyyy-mm-dd"T"hh24:mi:ss.ff"Z"')
from dual;
TO_TIMESTAMP('2017-11-16T04:4
-----------------------------
2017/11/16 04:45:05.000000000

Resources