Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET milliseconds with DATE_FORMAT in mysql

Tags:

mysql

I want to return a date with a certain format, for the moment I'm using:

SELECT DATE_FORMAT(NOW(3),'%y-%m-%d %H:%i:%s.%f')

which returns:

18-02-26 11:22:07.617000

that's perfect for now, however, I want to get Milleseconds, which means only 3 characters after the seconds, something like: 18-02-26 11:22:07.617 and I'm obliged to do it with DATE_FORMAT.

Do you have any idea?

like image 350
ziad sefraoui Avatar asked Feb 26 '18 10:02

ziad sefraoui


People also ask

Does MySQL TIMESTAMP have milliseconds?

No. It stores exactly what you asked for. It can store up to microsecond - but you have not asked for this accuracy. You call NOW() - and you get the datetime without milliseconds.

What is database TIMESTAMP with precision?

Timestamp Data Types. The TIMESTAMP data type consists of a date and time, with optional time zone. (Optional) Indicates the number of digits of precision in the fractions of seconds, as an integer value from 0 to 9. The default is 6.

How do you select all records that are 10 minutes with current TIMESTAMP in SQL Server?

SELECT col1, col2, col3 FROM table WHERE DATE_ADD(last_seen, INTERVAL 10 MINUTE) >= NOW(); This adds 10 minutes to last_seen and compares that against the current time. If the value is greater, last_seen is less than ten minutes ago. See the documentation on DATE_ADD() for an explanation of how it is used.


Video Answer


2 Answers

If you really want use DATE_FORMAT you can do this:

SELECT SUBSTRING(DATE_FORMAT(NOW(3), '%d-%b-%y %H:%m:%s.%f'),1,22);
like image 84
Léo R. Avatar answered Nov 15 '22 07:11

Léo R.


I was not able to comment on Leo R's answer. There is a typo at the second "%m".

The correct format would be:

SELECT SUBSTRING(DATE_FORMAT(NOW(3), '%d-%b-%y %H:%i:%s.%f'),1,22);
like image 43
dailabala Avatar answered Nov 15 '22 07:11

dailabala