I have a column called today
and the type is DATE
.
When I try to add the date in the format '07-25-2012'
I get the following error:
Unable to run query:Incorrect date value: '07-25-2012' for column
The obvious way to fix the error is to change the formatting of your value into the format that MySQL can accept. But rather than editing the value manually, you can use the STR_TO_DATE() function to help you convert the string value into date value.
Example: For converting datetime to format – dd:mm:yyyy, you can use the following query: SELECT DATE_FORMAT('2020-03-15 07:10:56.123', '%d:%m:%Y');
You just need to go to the Wamp panel, then to MySQL, then to settings and change the mode to sql-mode: none.
The STR_TO_DATE() function returns a date based on a string and a format.
As MySql accepts the date in y-m-d format in date type column, you need to STR_TO_DATE
function to convert the date into yyyy-mm-dd format for insertion in following way:
INSERT INTO table_name(today)
VALUES(STR_TO_DATE('07-25-2012','%m-%d-%y'));
Similary, if you want to select the date in different format other than Mysql format, you should try DATE_FORMAT
function
SELECT DATE_FORMAT(today, '%m-%d-%y') from table_name;
Generally mysql uses this date format 'Y-m-d H:i:s'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With