Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert time 2009-09-22 18:09:37.881 in MYSQL My column type is DateTime

Tags:

mysql

How to insert time 2009-09-22 18:09:37.881 in mysql. Actually I can insert and retrieve the time 2009-09-22 18:09:37 in mysql but whenever I am trying to insert 2009-09-22 18:09:37.881 data did not get inserted in database.

2009-09-22 18:09:37.881     ------->  YYYY-MM-DD HH:MI:Sec.Ms

I have created a table using the below query

Create table XYZ(MyTime DateTime);

I tried the below query which worked fine

insert into XYZ(MyTime) values('2009-09-22 18:09:37');

But I tried with the below query which did not work fine (Data didnot get insert in database)

 insert into XYZ(MyTime) values('2009-09-22 18:11:38.881');
like image 361
Sunil Kumar Sahoo Avatar asked Sep 22 '09 12:09

Sunil Kumar Sahoo


3 Answers

I got the issue resolved. The database was not allowing the insertion of time in millisecond. Please have a look at the lines, below:

CREATE TABLE MyTimeStamp(TimeData decimal(17,3));

INSERT INTO  MyTimeStamp(TimeData) values (20090922201843.426);

SELECT timestamp(TimeData) FROM MyTimeStamp;

OutPut:

2009-09-22 20:018:43.426000
like image 149
Sunil Kumar Sahoo Avatar answered Oct 21 '22 05:10

Sunil Kumar Sahoo


The fractional second capability was finally added in MySQL 5.6.4. Link to documentaion: http://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html

like image 36
Dale Avatar answered Oct 21 '22 05:10

Dale


Having had a quick scan on the MySQL reference pages here, they seem to suggest that you cannot add milliseconds to a column of type datetime. The direct quote is

"However, microseconds cannot be stored into a column of any temporal data type. Any microseconds part is discarded."

This implies that you cannot use Timestamp either.

Are the miliseconds necessary? Could they be stored in a secondary column and then recombined with the basic datetime upon retrieval?

like image 1
chillysapien Avatar answered Oct 21 '22 03:10

chillysapien