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
DATETIMEliteral within a MySQL statement?A: In MySQL, the standard format for a
DATETIMEliteral is: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_DATEwhich can convert strings in various formats toDATEorDATETIMEdatatypes.So, as an alternative, you can also specify the value of a
DATETIMEwith a call to that function, like this:So, you could have MySQL do the conversion for you in the
INSERTstatement, if yourVALUESlist looked like this:(I notice you have a required comma missing between the two literals in your
VALUESlist.)MySQL does allow some latitude in the delimiters between the parts of the
DATETIMEliteral, so they are not strictly required.MySQL 5.5 Reference Manual.