如何在PL / SQL中获得时代?

时间:2015-05-13 10:05:50

标签: sql oracle

我想以毫秒(epoch)的形式将当前时间插入表中。有没有我可以使用的内置功能,这给了我时代的时间?

2 个答案:

答案 0 :(得分:4)

这将给你自1970年1月1日以来的毫秒:

select(sysdate - to_date('01 -Jan-1970','dd-Mon-yyyy'))* 24 * 60 * 60 * 1000 from dual

答案 1 :(得分:1)

请记住时区

SQL> SELECT
  2     EXTRACT(DAY FROM (from_tz(CAST(SYSDATE AS TIMESTAMP), SESSIONTIMEZONE) AT TIME ZONE 'UTC' -TIMESTAMP '1970-01-01 00:00:00 +00:00'))*86400+
  3     EXTRACT(HOUR FROM (from_tz(CAST(SYSDATE AS TIMESTAMP), SESSIONTIMEZONE) AT TIME ZONE 'UTC' -TIMESTAMP '1970-01-01 00:00:00 +00:00'))*3600+
  4     EXTRACT(MINUTE FROM (from_tz(CAST(SYSDATE AS TIMESTAMP), SESSIONTIMEZONE) AT TIME ZONE 'UTC' -TIMESTAMP '1970-01-01 00:00:00 +00:00'))*60+
  5     EXTRACT(SECOND FROM (from_tz(CAST(SYSDATE AS TIMESTAMP), SESSIONTIMEZONE) AT TIME ZONE 'UTC' -TIMESTAMP '1970-01-01 00:00:00 +00:00')) date_to_epoch
  6  FROM dual;

DATE_TO_EPOCH
-------------
   1431513604