plsql日期为毫秒数

时间:2018-06-12 14:06:37

标签: plsql

PLSQL转换日期是否有可能达到毫秒数?

我试过

select to_number(TO_CHAR(sysdate, 'yyyymmddhh24miss')) * (24 * 60 * 60 * 1000) from dual;

但我回来了1743604888943174400000

与java GetTime方法不同 https://www.w3resource.com/java-tutorial/util/date/java_date_gettime.php

2 个答案:

答案 0 :(得分:0)

正如swmcdonnell已经说过,你的问题有点被公开: oracle systimestamp (sysdate) to milliseconds

但是你有一个理解问题。我确实将您的查询拆分为三列。

SELECT TO_CHAR (SYSDATE, 'yyyymmddhh24miss'), -- Here we convert our sysdate into a 'string'
       TO_NUMBER (TO_CHAR (SYSDATE, 'yyyymmddhh24miss')), -- this will output the resulting string as number - it will look the same as the 1st column
       TO_NUMBER (TO_CHAR (SYSDATE, 'yyyymmddhh24miss')) * (24 * 60 * 60 * 1000) -- here you calculated 20180613150101 * 24 * 60... i think that's not what you want to do
  FROM DUAL;

这没有意义。 如果你想要'total-milliseconds',你必须:

  1. 使用current_timestamp而不是sysdate
  2. 将年份(yyyy)乘以365天* 24小时* 60分钟* 60秒* 1000毫秒
  3. 将日期(DDD)乘以24小时* 60分钟* 60秒* 1000毫秒
  4. 乘以小时* 60分钟* 60秒* 1000毫秒
  5. 乘以分钟* 60秒* 1000毫秒
  6. 乘以第二个* 1000毫秒
  7. 添加ms
  8. 这会导致类似这样的事情:

    SELECT to_number(TO_CHAR (CURRENT_TIMESTAMP, 'yyyy')) * 365 * 24 * 60 * 60 * 1000
         + to_number(TO_CHAR (CURRENT_TIMESTAMP, 'DDD')) * 24 * 60 * 60 * 1000
         + to_number(TO_CHAR (CURRENT_TIMESTAMP, 'HH24')) * 60 * 60 * 1000
         + to_number(TO_CHAR (CURRENT_TIMESTAMP, 'mi')) * 60 * 1000
         + to_number(TO_CHAR (CURRENT_TIMESTAMP, 'ss')) * 1000
         + to_number(TO_CHAR (CURRENT_TIMESTAMP, 'FF'))
      FROM DUAL;
    

    ..但你会怎么做呢?我不知道Java但是时间戳不应该显示为间隔。这可能类似于oracle中的interval或C#中的时间跨度。

答案 1 :(得分:0)

SELECT TO_NUMBER(TO_CHAR(SYSDATE,'YYYYMMDDHHMISS'))||to_number(SUBSTR(TO_CHAR (CURRENT_TIMESTAMP, 'FF'),0,2)) FROM dual;