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?
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.
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.
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.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With