tags:

views:

162

answers:

3

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');

Thanks Sunil Kumar Sahoo

A: 

All time types in MySQL have only a precision of seconds

[...] microseconds cannot be stored into a column of any temporal data type. Any microseconds part is discarded.

You'll need some workaround.

Aaron Digulla
I created table using the command CREATE TABLE MyTimeZone (MyTime timestamp). Then I inserted timestamp using the following query insert into MyTimeZone(MyTime) values('2009-09-22 19:05:02.897');Then I retrieved the data using the follwing commandSelect * from MyTimeZoneI received the time zone as 2009-09-22 19:05:02 I did not receive the exact time stamp that I have inserted. My time stamp includes milliseconds also
Deepak
Timestamp also only has a precision of seconds. 5th paragraph here: http://dev.mysql.com/doc/refman/5.0/en/datetime.html
Evernoob
Actually I want to store millisecond also in table
Deepak
@Evernoob: Great ... :( Now I've seen DATE columns which have a time, databases which don't support TIME at all ... and now that.
Aaron Digulla
+1  A: 

Hey there,

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?

chillysapien
+1  A: 

Hi I got the issue resolved. Database will not allow to store time in millisecond. please have a look on the below line

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

Thanks Sunil Kumar Sahoo

Deepak