Ksenia
Ksenia

Reputation: 3731

How to get current timestamp with milliseconds in MySQL?

I'm able to get approximate time without milliseconds by query

SELECT UNIX_TIMESTAMP(NOW());

but how to get precise current timestamp with milliseconds? For example, after executing the above query I get 1352717640 value, but I want to get 1352717640xxx value.

UPD

And other answers from SO (like this) offers solution like

SELECT UNIX_TIMESTAMP(NOW(3))*1000;

but this query only fills up milliseconds place with '000', but I want to get PRECISE value of milliseconds, like 1352717640937.

In SQLyog:

enter image description here

And in mysql console:

enter image description here

Is it possible at all for MySQL 5.1?

Upvotes: 6

Views: 9436

Answers (2)

MTK
MTK

Reputation: 3580

Mysql 8.0.26

SELECT UNIX_TIMESTAMP(NOW(3))*1000;
-- return float 1672434492831.000

To integer:

SELECT FLOOR(UNIX_TIMESTAMP(NOW(3))*1000);
-- return INT 1672434492831

Upvotes: 2

Daniel E.
Daniel E.

Reputation: 2480

I think you didn't test it, but yes the solution is

SELECT UNIX_TIMESTAMP(NOW(3))*1000;

SQL Fiddle

MySQL 5.6 Schema Setup:

Query 1:

SELECT UNIX_TIMESTAMP(NOW(3))*1000

Results:

| UNIX_TIMESTAMP(NOW(3))*1000 |
|-----------------------------|
|               1519827493419 |

Upvotes: 6

Related Questions