CURRENT_TIMESTAMP in milliseconds

后端 未结 19 885
失恋的感觉
失恋的感觉 2020-12-07 23:48

Is there any way to get milliseconds out of a timestamp in MySql or PostgreSql (or others just out of curiosity)?

SELECT CURRENT_TI         


        
相关标签:
19条回答
  • 2020-12-08 00:32

    Use:

    Select curtime(4);
    

    This will give you milliseconds.

    0 讨论(0)
  • 2020-12-08 00:35

    In Mysql 5.7+ you can execute

    select current_timestamp(6)
    

    for more details

    https://dev.mysql.com/doc/refman/5.7/en/fractional-seconds.html

    0 讨论(0)
  • 2020-12-08 00:35

    Here's an expression that works for MariaDB and MySQL >= 5.6:

    SELECT (UNIX_TIMESTAMP(NOW()) * 1000000 + MICROSECOND(NOW(6))) AS unix_now_in_microseconds;
    

    This relies on the fact that NOW() always returns the same time throughout a query; it's possible that a plain UNIX_TIMESTAMP() would work as well, I'm not sure based on the documentation. It also requires MySQL >= 5.6 for the new precision argument for NOW() function (MariaDB works too).

    0 讨论(0)
  • 2020-12-08 00:37

    The correct way of extracting miliseconds from a timestamp value on PostgreSQL accordingly to current documentation is:

    SELECT date_part('milliseconds', current_timestamp);
    
    --OR
    
    SELECT EXTRACT(MILLISECONDS FROM current_timestamp);
    

    with returns: The seconds field, including fractional parts, multiplied by 1000. Note that this includes full seconds.

    0 讨论(0)
  • 2020-12-08 00:37

    In PostgreSQL we use this approach:

    SELECT round(EXTRACT (EPOCH FROM now())::float*1000)
    
    0 讨论(0)
  • 2020-12-08 00:46

    For MySQL (5.6+) you can do this:

    SELECT ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000)
    

    Which will return (e.g.):

    1420998416685 --milliseconds
    
    0 讨论(0)
提交回复
热议问题