I want to get milliseconds from date field of oracle for date "01-01-9999".
I have created below block to achieve the same.
set serveroutput on;
declare
base_point constant timestamp := to_timestamp_tz('01-JAN-1970 00:00:00.000+00:00', 'DD-Mon-RR HH24:MI:SS.FFTZH:TZM') AT TIME ZONE 'UTC';
now timestamp := to_timestamp_tz('01-01-2099 00:00:00.000+00:00', 'DD-MM-RR HH24:MI:SS.FFTZH:TZM') AT TIME ZONE 'UTC';
-- now constant timestamp := systimestamp AT TIME ZONE 'UTC' ;
n number;
begin
select to_timestamp_tz(to_char(todate,'DD-MM-YY HH24:MI:SS')||'.000+00:00','DD-MM-YY HH24:MI:SS.FFTZH:TZM')
into now
from t_table where ACCOUNTID = 'ACC001124211';
DBMS_OUTPUT.put_line(' now :'||now);
n := (
((extract(day from (now-base_point)))*86400)
+ ((extract(hour from (now-base_point)))*3600)
+ ((extract(minute from (now-base_point)))*60)
+ ((extract(second from (now-base_point))))
) * 1000;
DBMS_OUTPUT.put_line(' n :'||n);
end;
/
but using above block I am getting value as 4070908800000
, which is equal to date 1/1/2099
but actual date in my table is 01-01-9999
Can you please help us to get exact millisecond using date field
Oracle date data types do not support milliseconds but Oracle timestamps do.
Oracle has expanded on the DATE datatype and has given us the TIMESTAMP datatype which stores all the information that the DATE datatype stores, but also includes fractional seconds. If you want to convert a DATE datatype to a TIMESTAMP datatype format, just use the CAST function.
Oracle introduced TIMESTAMP data type in 9i version. It allows to store date time with fraction of seconds. By default format of TIMESTAMP is 'YYYY-MM-DD HH24:MI:SS. FF'.
Although Oracle's date does contain a time, it only stores seconds, not milliseconds. To get the fractional seconds from a timestamp use to_char () and convert that to a number: Show activity on this post.
Our application needs to get a date value including millisecond in PL/SQL packages,but the date datatype in Oracle doesn't support millisecond. Do you have some ideas to solve this kind of problem?
To get the current date and time on the Database Server Oracle has SYSDATE internal value which returns the current date from the operating system on which the database resides. The datatype of the returned value is DATE, and the format returned depends on the value of the NLS_DATE_FORMAT initialization parameter.
To get current date and time in Oracle SYSDATE internal value which returns the current date from the operating system on which the database resides. The datatype of the returned value is DATE, and the format returned depends on the value of the NLS_DATE_FORMAT initialization parameter.
No need of PL/SQL, you could do it in plain SQL.
To convert a date to milliseconds since 01-JAN-1970
:
SQL> SELECT to_number(DATE '9999-01-01'
2 - to_date('01-JAN-1970','DD-MON-YYYY')) * (24 * 60 * 60 * 1000) milliseconds
3 FROM dual;
MILLISECONDS
------------------
253370764800000
SQL>
The reason why you are getting wrong value is this statement.
select to_timestamp_tz(to_char(todate,'DD-MM-YY HH24:MI:SS')||'.000+00:00','DD-MM-YY HH24:MI:SS.FFTZH:TZM')
Since your format element for year is YY, to_char conversion will have only 2 digits for year.
select to_char(date'9999-01-01','DD-MM-YY HH24:MI:SS')||'.000+00:00' char_date
from dual
char_date
------------
01-01-99 00:00:00.000+00:00
When you convert this to timestamp using YY as format element, the year returned always has the same first 2 digits as the current year, which is why you get 2099 as year.
select to_char(to_timestamp_tz(to_char(date'9999-01-01','DD-MM-YY HH24:MI:SS')||'.000+00:00','DD-MM-YY HH24:MI:SS.FFTZH:TZM'),'yyyy') char_date
from dual;
char_date
------------
2099
Moral of the story:
Oracle recommends that you use the 4-digit year element (YYYY) instead of the shorter year elements for these reasons:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With