Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite: Insert current timestamp with milliseconds precision

Tags:

sql

sqlite

I'm trying to insert the current timestamp into SQLite:

CREATE TABLE test (timestamp DATETIME);
INSERT INTO test VALUES(datetime('now'));

This does create the timestamp, but only with seconds-precision (the timestamp looks like 2013-12-17 07:02:20). Is it possible to add the milliseconds, as well?

like image 955
sashoalm Avatar asked Dec 17 '13 09:12

sashoalm


People also ask

How do I insert the current date and time in SQLite?

First, create a new table named datetime_real . Second, insert the “current” date and time value into the datetime_real table. We used the julianday() function to convert the current date and time to the Julian Day. Third, query data from the datetime_real table.

What is timestamp precision?

Precision in time values specifies the number of fractional digits retained in the seconds field. For example, specifying a TIMESTAMPTZ value as TIMESTAMPTZ(3) truncates the time component to milliseconds. By default, TIMESTAMP / TIMESTAMPTZ values have a precision of 6 (microseconds).

How do I get milliseconds MySQL?

MySQL MICROSECOND() function MySQL MICROSECOND() returns MICROSECONDs from the time or datetime expression. The return value is within the range of 0 to 999999.


1 Answers

Date and time functions reference says datetime(timestring, [modifiers...]) is equivalent to strftime('%Y-%m-%d %H:%M:%S', timestring, [modifiers...]).

If you want fractional seconds you must use %f not %S. So use strftime('%Y-%m-%d %H:%M:%f', 'now').

like image 173
Benoit Avatar answered Sep 22 '22 05:09

Benoit