mysql

Insert custom date into mysql timestamp field


I try to insert values into a new table. This is the insert with certain date format (dd/mm/yyyy h:m):

INSERT INTO `sport` (`TIME`, `HOME`, `VISIT`, `HOME_RESULT`, `VISIT_RESULT`) VALUES ('20/07/2024 19:15','Dortmund','Bayern','5','3');

and this is the create table scheme:

CREATE TABLE `sport` (
  `ID` MEDIUMINT NOT NULL AUTO_INCREMENT,
  `TIME` datetime NOT NULL,
  `HOME` varchar(200) NOT NULL,
  `VISIT` varchar(200) NOT NULL,
  `HOME_RESULT` int(3) NOT NULL,
  `VISIT_RESULT` int(3) NOT NULL,
  PRIMARY KEY (ID)
);

But I get the following error:

ERROR 1292 (22007): Incorrect datetime value: '20/07/2024 19:15' for column `sport`.`sport`.`TIME` at row 1

How can I fix this problem with datetime format?

Thanks in advance


Solution

  • you should use the YYYY-MM-DD HH:MM:SS format:

    INSERT INTO sport (TIME, HOME, VISIT, HOME_RESULT, VISIT_RESULT)
    VALUES ('2024-07-20 19:15:00', 'Home Team', 'Visit Team', 3, 2);