Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while inserting date - Incorrect date value:

Tags:

sql

mysql

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

like image 827
Illep Avatar asked Jul 25 '12 00:07

Illep


People also ask

How do I fix incorrect date value?

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.

How do I insert date in YYYY MM DD format in MySQL?

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

How to solve error 1292 in MySQL?

You just need to go to the Wamp panel, then to MySQL, then to settings and change the mode to sql-mode: none.

What is STR_TO_DATE in MySQL?

The STR_TO_DATE() function returns a date based on a string and a format.


2 Answers

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; 
like image 142
Akash KC Avatar answered Oct 07 '22 12:10

Akash KC


Generally mysql uses this date format 'Y-m-d H:i:s'

like image 4
aserwin Avatar answered Oct 07 '22 12:10

aserwin