开发者

Date insert into mysql table

开发者 https://www.devze.com 2023-01-28 19:57 出处:网络
My sql insert was date(%d-%M-%y) but that has zero effect on the field no data was added. Neither did inserting 02-Dec-2010. What do I have to c开发者_StackOverflow中文版hange the table field Type or

My sql insert was date(%d-%M-%y) but that has zero effect on the field no data was added. Neither did inserting 02-Dec-2010. What do I have to c开发者_StackOverflow中文版hange the table field Type or Default to so that the date inserted will be in 02-Dec-2010 format?

Field: creationdate
Type: timestamp 
Default: CURRENT_TIMESTAMP (showing 0000-00-00 00:00:00)


You don't pre-format your dates. You insert a timestamp formatted like YYYY-MM-DD HH:II:SS and when you need to format your date (SELECT statement), you use DATE_FORMAT function.

If you already have a timestamp like 02-Dec-2010, you can use STR_TO_DATE function to convert it to a real timestamp.

mysql> SELECT DATE_FORMAT(current_date, '%d-%b-%Y');
+--------------------------------+
| DATE_FORMAT(NOW(), '%d-%b-%Y') |
+--------------------------------+
| 02-Dec-2010                    |
+--------------------------------+

mysql> SELECT STR_TO_DATE('02-Dec-2010', '%d-%b-%Y');
+----------------------------------------+
| STR_TO_DATE('02-Dec-2010', '%d-%b-%Y') |
+----------------------------------------+
| 2010-12-02                             |
+----------------------------------------+


MySQL expects date / datetime datatypes to contain dates and/or timestamps that have the date in the YYYY-MM-DD format where - is any valid separator. 02-Dec-2010 is not a valid date for those types.

0

精彩评论

暂无评论...
验证码 换一张
取 消