Datetime & Timestamp manipulation / migration explained
Jun 15, 2010
What if its PM rather than AM
SELECT DATE_FORMAT(STR_TO_DATE(‘MAR 16 2008 09:12:51:893PM’,'%M %d %Y %h:%i:%s:%f%p’),’%Y%m%d%k%i%s.%f’); –> 20080316211251.893000
Ok so this is just simple string manipulation where:
%M is the month name
%d is day number
%Y is the year
%h is the hour
%i is the minute
%s is the second
%f is the microsecond
%p is the period: ante or post meridiem
In the DATE_FORMAT part we se a %k which is in 24hr format in order to loose the period.
A more detailed list is found here
Here is a demo:
mysql Tue Jun 15 12:32:37 2010 > CREATE TABLE test.abc(a DECIMAL(17,3)) ENGINE=MYISAM;
Query OK, 0 rows affected (0.03 sec)
mysql Tue Jun 15 12:32:45 2010 > INSERT INTO abc VALUES ( DATE_FORMAT(STR_TO_DATE(‘MAR 16 2008 09:12:51:893PM’,'%M %d %Y %h:%i:%s:%f%p’),’%Y%m%d%k%i%s.%f’) );
Query OK, 1 row affected (0.01 sec)
mysql Tue Jun 15 12:32:51 2010 > SELECT * FROM abc;
+——————–+
| a ? ? ? ? ? ? ? ? ?|
+——————–+
| 20080316211251.893 |
+——————–+
1 row in set (0.00 sec)
mysql Tue Jun 15 12:32:56 2010 > SELECT TIMESTAMP(a) FROM abc;
+—————————-+
| TIMESTAMP(a) ? ? ? ? ? ? ? |
+—————————-+
| 2008-03-16 21:12:51.893000 |
+—————————-+
1 row in set (0.00 sec)

