oracle systimestamp (sysdate) to milliseconds

前端 未结 7 1246
予麋鹿
予麋鹿 2020-12-16 14:15

Could you provide implementation of stored function to get current systimestamp as milliseconds.
Something I can use like

select current_tim         


        
相关标签:
7条回答
  • 2020-12-16 14:51
    • 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;
    
    0 讨论(0)
  • 2020-12-16 14:52

    I've posted here some methods to convert timestamp to nanoseconds and nanoseconds to timestamp. These methods are not affected by time zones and have a nanosecond precision.

    You just need to adjust it to get milliseconds instead of nanoseconds.

    SELECT (EXTRACT(DAY FROM (
        SYSTIMESTAMP --Replace line with desired timestamp --Maximum value: TIMESTAMP '3871-04-29 10:39:59.999999999 UTC'
    - TIMESTAMP '1970-01-01 00:00:00 UTC') * 24 * 60) * 60 + EXTRACT(SECOND FROM
        SYSTIMESTAMP --Replace line with desired timestamp
    )) *  1000 AS MILLIS FROM DUAL;
    
    MILLIS
    1598434427263.027
    
    0 讨论(0)
  • 2020-12-16 14:56

    AFAIK, there is no direct way for achieving this (other than manually writing a long-winded SQL function).

    Why do you need this specifically?

    You could use a stored Java function and then use the System.getCurrentMillis() that Java provides to return you a value in Milliseconds from 1.1.1970 to now.

    0 讨论(0)
  • 2020-12-16 15:05
    SELECT to_char(sysdate, 'HH24:MI:SS'), to_char(systimestamp, 'HH24:MI:SS.FF6') FROM dual
    
    0 讨论(0)
  • 2020-12-16 15:08

    Below code gives the difference in milliseconds:

    with t as (select systimestamp - to_timestamp(sysdate ) diff from dual)
    select extract(day from diff) * 24 * 3600000+
       extract(hour from diff) * 3600000+
       extract(minute from diff) * 60000 +
       extract(second from diff) * 1000  
        dif
    from t
    

    For conversion of milliseconds to Hours, Minutes, seconds, modify and use below query as appropriate:

    with t as (select systimestamp - to_timestamp(sysdate ) diff from dual)
    select extract(day from diff) * 24 * 3600000+
       extract(hour from diff) * 3600000+
       extract(minute from diff) * 60000 +
       extract(second from diff) * 1000  
        dif,
        (to_char (to_date(round(( extract(day from diff) * 24 * 3600000+
       extract(hour from diff) * 3600000+
       extract(minute from diff) * 60000 +
       extract(second from diff) * 1000)/1000), 'SSSSS' ), 'HH24"Hrs" MI"Min" SS"Sec"'))  timeval
    from t
    
    0 讨论(0)
  • 2020-12-16 15:10

    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';
    /
    
    0 讨论(0)
提交回复
热议问题