Error while inserting date - Incorrect date value:

前端 未结 7 1692
花落未央
花落未央 2020-12-09 08:31

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

相关标签:
7条回答
  • 2020-12-09 08:54

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

    0 讨论(0)
  • 2020-12-09 08:56

    You need to convert the date to YYYY-MM-DD in order to insert it as a MySQL date using the default configuration.

    One way to do that is STR_TO_DATE():

    insert into your_table (...)
    values (...,str_to_date('07-25-2012','%m-%d-%Y'),...);
    
    0 讨论(0)
  • 2020-12-09 08:58

    you need to use YYYY-MM-DD format to insert date in mysql

    0 讨论(0)
  • 2020-12-09 08:59

    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; 
    
    0 讨论(0)
  • 2020-12-09 09:09

    This is the date format:

    The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

    Why do you insert '07-25-2012' format when MySQL format is '2012-07-25'?. Actually you get this error if the sql_mode is traditional/strict mode else it just enters 0000-00-00 and gives a warning: 1265 - Data truncated for column 'col1' at row 1.

    0 讨论(0)
  • 2020-12-09 09:09

    You can use "DATE" as a data type while you are creating the table. In this way, you can avoid the above error. Eg:

    CREATE TABLE Employee (birth_date DATE);
    INSERT INTO Employee VALUES('1967-11-17');
    
    0 讨论(0)
提交回复
热议问题