datetime mysql 格式,MySQL数据库的正确DateTime格式是什么?

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');

and I receive this error message:

Incorrect datetime value: '12/21/2012 1:13:58 PM' for column 'stock_dateUpdated' at row 1

So what is the right format/value for dateTime to input into a MySQL database?

解决方案

Q: What is the right format/value for DATETIME literal within a MySQL statement?

A: In MySQL, the standard format for a DATETIME literal is:

'YYYY-MM-DD HH:MI:SS'

with the time component as a 24 hour clock (i.e., the hours digits provided as a value between 00 and 23).

MySQL provides a builtin function STR_TO_DATE which can convert strings in various formats to DATE or DATETIME datatypes.

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')

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');

(I notice you have a required comma missing between the two literals in your VALUES list.)

MySQL does allow some latitude in the delimiters between the parts of the DATETIME literal, so they are not strictly required.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值