我正在使用此 SQL 字符串将此 DateTime 数据 '12/21/2012 1:13:58 PM' 插入我的 MySQL 数据库:
I am Inserting this DateTime data '12/21/2012 1:13:58 PM' into my MySQL Database using this SQL string:
String Query = "INSERT INTO `restaurantdb`.`stocksdb`
(`stock_ID`,`stock_dateUpdated`)
VALUES ('@stockID' '@dateUpdated');
我收到此错误消息:
Incorrect datetime value: '12/21/2012 1:13:58 PM' for column 'stock_dateUpdated' at row 1
那么 dateTime 输入 MySQL 数据库的正确格式/值是什么?
So what is the right format/value for dateTime to input into a MySQL database?
问: MySQL 语句中 DATETIME 文字的正确格式/值是什么?
Q: What is the right format/value for DATETIME literal within a MySQL statement?
p>
答: 在 MySQL 中,DATETIME 文字的标准格式是:
A: In MySQL, the standard format for a DATETIME literal is:
'YYYY-MM-DD HH:MI:SS'
时间组件为 24 小时制(即,小时数字作为 00 到 23 之间的值提供).
with the time component as a 24 hour clock (i.e., the hours digits provided as a value between 00 and 23).
MySQL提供了一个内置函数STR_TO_DATE,可以将各种格式的字符串转换为DATE或DATETIME数据类型.
MySQL provides a builtin function STR_TO_DATE which can convert strings in various formats to DATE or DATETIME datatypes.
因此,作为替代方案,您还可以通过调用该函数来指定 DATETIME 的值,如下所示:
So, as an alternative, you can also specify the value of a DATETIME with a call to that function, like this:
STR_TO_DATE('12/21/2012 1:13:58 PM','%m/%d/%Y %h:%i:%s %p')
因此,如果您的 VALUES 列表如下所示,您可以让 MySQL 在 INSERT 语句中为您进行转换:
So, you could have MySQL do the conversion for you in the INSERT statement, if your VALUES list looked like this:
... VALUES ('@stockID', STR_TO_DATE('@dateUpdated','%m/%d/%Y %h:%i:%s %p');
(我注意到您的 VALUES 列表中的两个文字之间缺少必需的逗号.)
(I notice you have a required comma missing between the two literals in your VALUES list.)
MySQL 确实允许 DATETIME 文字部分之间的分隔符有一定的自由度,因此它们不是严格要求的.
MySQL does allow some latitude in the delimiters between the parts of the DATETIME literal, so they are not strictly required.
MySQL 5.5 参考手册.
这篇关于MySQL 数据库的正确日期时间格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
车牌检测有哪些好的算法?What are good algorithms for vehicle license plate detection?(车牌检测有哪些好的算法?)
Unity中图像的onClick事件onClick event for Image in Unity(Unity中图像的onClick事件)
运行总 C#Running Total C#(运行总 C#)
单击带有 JAvascript.ASP.NET C# 的超链接时删除目录Deleting a directory when clicked on a hyperlink with JAvascript.ASP.NET C#(单击带有 JAvascript.ASP.NET C# 的超链接时删除目录)
asp.net listview 在单击时突出显示行asp.net listview highlight row on click(asp.net listview 在单击时突出显示行)
从函数调用按钮 OnClickCalling A Button OnClick from a function(从函数调用按钮 OnClick)