oracle systimestamp (sysdate) to milliseconds

  • DB timezone agnostic
  • with milliseconds
  • works in XE
    function current_time_ms
        return number
    is
        out_result number;
    begin
        select extract(day from(sys_extract_utc(systimestamp) - to_timestamp('1970-01-01', 'YYYY-MM-DD'))) * 86400000 
            + to_number(to_char(sys_extract_utc(systimestamp), 'SSSSSFF3'))
        into out_result
        from dual;
        return out_result;
    end current_time_ms;

The best thing I know of is:

select extract(day    from (systimestamp - timestamp '1970-01-01 00:00:00')) * 86400000
     + extract(hour   from (systimestamp - timestamp '1970-01-01 00:00:00')) * 3600000
     + extract(minute from (systimestamp - timestamp '1970-01-01 00:00:00')) * 60000
     + extract(second from (systimestamp - timestamp '1970-01-01 00:00:00')) * 1000 unix_time
from dual;

I'm not quite sure what requirements you have regarding time zone. You might need to make minor adjustments for that.


Adding to @Mykhaylo Adamovych answer (which looks correct!) here goes a more straightforward approach using oracle Java support (i.e. not in XE and not in AWS RDS). Less portable (in case you care), but seemed faster in my testing.

CREATE or replace FUNCTION current_java_timestamp RETURN number
AS LANGUAGE JAVA NAME 'java.lang.System.currentTimeMillis() return java.lang.Long';
/