What is the correct DateTime format for MySQL Database?(MySQL 数据库的正确日期时间格式是什么?)
问题描述
我正在使用此 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 数据库的正确日期时间格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL 数据库的正确日期时间格式是什么?
基础教程推荐
- C# 从 List<List<int>> 中删除重 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
