How to Return the Unix Timestamp in Oracle

Here’s an option for returning the Unix timestamp when using Oracle Database.

The Unix timestamp (also known as Unix Epoch time, Unix time, or POSIX time) is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970, Coordinated Universal Time (UTC).

Get the Current Unix Timestamp

Here’s an example of getting the Unix timestamp from the current date and time:

SELECT (CAST (systimestamp at time zone 'UTC' as date) - date '1970-01-01') * 86400
FROM DUAL;

Example result:

1650253093.999999999999999999999999999998

Convert a Specified Timestamp to Unix Timestamp

Here’s an example where the input timestamp is a hardcoded value:

SELECT (
    CAST(timestamp '2030-08-15 18:30:45' at time zone 'UTC' as date) - date '1970-01-01'
    ) * 86400
FROM DUAL;

Result:

1913074244.999999999999999999999999999997