Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

Convert MySQL datetime to timestamp

I am trying to convert datetime into timestamp but mysql is giving me warnings and also converted values are wrong. Here is SQL query

UPDATE table1 A, table2 B SET B.date_added=UNIX_TIMESTAMP(STR_TO_DATE(A.date_added, '%M %d %Y %h:%i%p')) WHERE A.id=B.id;

Warnings

+---------+------+--------------------------------------------------------------------------+
| Level   | Code | Message                                                                  |
+---------+------+--------------------------------------------------------------------------+
| Warning | 1411 | Incorrect datetime value: '2011-06-11 20:29:02' for function str_to_date |
+---------+------+--------------------------------------------------------------------------+

Result

+---------------------+---------------------+
| date_added          | date_added          |
+---------------------+---------------------+
| 2012-02-23 06:12:45 | 2012-12-23 19:08:33 |
+---------------------+---------------------+

I also tried following query but it shows 0000-00-00 00:00:00 in timestamp field.

UPDATE table1 A, table2 B SET B.date_added=UNIX_TIMESTAMP(A.date_added) WHERE A.id=B.id;
question from:https://stackoverflow.com/questions/14014641/convert-mysql-datetime-to-timestamp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UPDATE table1 A, table2 B SET B.date_added=UNIX_TIMESTAMP(A.date_added) WHERE A.id=B.id;

UNIX_TIMESTAMP('2015-01-15 12:00:00'); is sufficient to convert a mysql datetime to a Timestamp.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...