MySQL 数据库的正确日期时间格式是什么? [英] What is the correct DateTime format for MySQL Database?

查看:44
本文介绍了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,可以将各种格式的字符串转换为DATEDATETIME数据类型.

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 数据库的正确日期时间格式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆