問題描述
我正在使用此 SQL 字符串將此 DateTime
數(shù)據(jù) '12/21/2012 1:13:58 PM'
插入我的 MySQL 數(shù)據(jù)庫:
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 數(shù)據(jù)庫的正確格式/值是什么?
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
文字的標(biāo)準(zhǔn)格式是:
A: In MySQL, the standard format for a DATETIME
literal is:
'YYYY-MM-DD HH:MI:SS'
時間組件為 24 小時制(即,小時數(shù)字作為 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提供了一個內(nèi)置函數(shù)STR_TO_DATE
,可以將各種格式的字符串轉(zhuǎn)換為DATE
或DATETIME
數(shù)據(jù)類型.
MySQL provides a builtin function STR_TO_DATE
which can convert strings in various formats to DATE
or DATETIME
datatypes.
因此,作為替代方案,您還可以通過調(diào)用該函數(shù)來指定 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
語句中為您進(jìn)行轉(zhuǎn)換:
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
文字部分之間的分隔符有一定的自由度,因此它們不是嚴(yán)格要求的.
MySQL does allow some latitude in the delimiters between the parts of the DATETIME
literal, so they are not strictly required.
MySQL 5.5 參考手冊.
這篇關(guān)于MySQL 數(shù)據(jù)庫的正確日期時間格式是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!