Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get time in millisecond from date field of oracle for the date 01-01-9999

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

like image 542
user3812269 Avatar asked Mar 31 '16 03:03

user3812269


People also ask

Does Oracle TIMESTAMP have milliseconds?

Oracle date data types do not support milliseconds but Oracle timestamps do.

Can we convert date to TIMESTAMP in Oracle?

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.

What is the format of TIMESTAMP in Oracle?

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'.

How to get fractional seconds from a date in Oracle?

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.

Is it possible to get millisecond value in date datatype in Oracle?

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?

How do I get the current date and time in Oracle?

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.

How to get current date and time in Oracle SYSDATE internal value?

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.


2 Answers

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>
like image 67
Lalit Kumar B Avatar answered Oct 21 '22 06:10

Lalit Kumar B


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:

  • The 4-digit year element eliminates ambiguity.
  • The shorter year elements may affect query optimization because the year is not known at query compile time and can only be determined at run time.
like image 27
Noel Avatar answered Oct 21 '22 07:10

Noel