DEV Community

Cover image for Get the current unix timestamp in oracle
Adrian Matei for Codever

Posted on • Updated on • Originally published at codever.dev

Get the current unix timestamp in oracle

The following expression returns the Unix time in milliseconds independent of the time zone.

It sums the number of days passed from the start of the Unix epoch multiplied by the number of milliseconds in a day (24 * 60 * 60 * 1000 = 86400000) plus number of milliseconds past midnight (SSSSS with a precision of 3 FF3 to express milliseconds).

It uses the SYS_EXTRACT_UTC method to extract the UTC (Coordinated Universal Time—formerly Greenwich Mean Time) from the current timestamp (systimestamp in oracle):

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'))
  FROM dual;
Enter fullscreen mode Exit fullscreen mode


Reference -
https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SYS_EXTRACT_UTC.html


Shared with love from Bookmarks.dev. Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.

Top comments (0)