MySQL INSERT 到 DB DateTime 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10539154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:19:42  来源:igfitidea点击:

INSERT into DB DateTime string

mysql

提问by Anton Sementsov

I have the string 04/04/2012 04:03:35 AM. How can I convert it and insert into a table? Field type is datetime.

我有字符串04/04/2012 04:03:35 AM。如何将其转换并插入表格?字段类型为datetime.

回答by eggyal

You need to use STR_TO_DATE():

您需要使用STR_TO_DATE()

STR_TO_DATE('04/04/2012 04:03:35 AM', '%d/%m/%Y %r')

Or, better yet, present your literal in a format MySQL expects(e.g. YYYY-MM-DD HH:mm).

或者,更好的是,以 MySQL 期望的格式呈现您的文字(例如YYYY-MM-DD HH:mm)。