如何从日期 01-01-9999 的 oracle 的日期字段中获取以毫秒为单位的时间

use*_*269 6 database oracle oracle10g oracle11g

我想从日期“01-01-9999”的 oracle 的日期字段中获取毫秒数。

我创建了下面的块来实现相同的目的。

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;
/
Run Code Online (Sandbox Code Playgroud)

但是使用上面的块我得到的价值为4070908800000,它等于日期 1/1/2099但我表中的实际日期是01-01-9999

你能帮助我们使用日期字段获得精确的毫秒吗

Lal*_*r B 6

不需要PL/SQL ,您可以用普通SQL来完成。

要将日期转换为自以下时间起的毫秒数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>
Run Code Online (Sandbox Code Playgroud)