Unable to convert varchar to datetime in MySql

大憨熊 提交于 2019-12-24 03:29:38

问题


In my MySql table there's a column called _time of type varchar. The values it holds are in the format: year month day hour minute without the whitespaces: 201409201945 I want to convert it to datetime so I'm doing this:

ALTER TABLE `my_table` CHANGE COLUMN `_time` `date_time` DATETIME NOT NULL; 

And it throws this error for some reason:

Error Code: 1292. Incorrect datetime value: '201409201945' for column '_date_time' at row 1 0.036 sec


回答1:


The three steps @Arkain mentioned would be with the help of the function STR_TO_DATE

-- add the new column
ALTER TABLE `my_table` ADD COLUMN `date_time` DATETIME; 

-- update the new column with the help of the function STR_TO_DATE
UPDATE `my_table` SET `date_time` = STR_TO_DATE(`_time`, '%Y%m%d%H%i');

-- drop the old column
ALTER TABLE `my_table` DROP COLUMN `_time`;

The complete list of specifiers for STR_TO_DATE can be found at DATE_FORMAT, here an excerpt with those I used:

%d  Day of the month, numeric (00..31)
%H  Hour (00..23)
%i  Minutes, numeric (00..59)
%m  Month, numeric (00..12)
%Y  Year, numeric, four digits

Demo of the UPDATE

If the new column should have the attribute NOT NOLL, one way could be to set the sql mode before the operation to '' and reset the sql_mode later on:

SET @old_mode = @@sql_mode;
SET @@sql_mode = '';        -- permits zero values in DATETIME columns

ALTER TABLE `my_table` ADD COLUMN `date_time` DATETIME NOT NULL; 
UPDATE `my_table` SET `date_time` = STR_TO_DATE(`_time`, '%Y%m%d%H%i');
ALTER TABLE `my_table` DROP COLUMN `_time`;

SET @@sql_mode = @old_mode;

Updated Demo




回答2:


If your varchar data were formatted like this '2014-09-20 19:45' altering your column's data type would work. Why? that's the character representation used by DATETIME and other time-oriented data types.

But it isn't. So, what choices do you have? One is to use these four steps:

  1. alter the table to add a new DATETIME column with a temporary name
  2. do an UPDATE with no WHERE clause to fill in the values of that column
  3. alter the table to drop the previous column
  4. alter the table to rename your new column to have the same name as the column you just dropped.

Here's how that would go.

ALTER TABLE my_table ADD COLUMN tempstamp DATETIME
UPDATE my_table SET tempstamp = STR_TO_DATE(_time, '%Y%m%d%H%i')
ALTER TABLE my_table DROP COLUMN _time
ALTER TABLE my_table CHANGE tempstamp _time DATETIME NOT NULL

Another approach: Change the strings in your _time to valid datetime values, then alter your column. If your varchars() are wide enough to hold a few extra characters, try this.

UPDATE my_table SET `_time`=STR_TO_DATE(`_time`, '%Y%m%d%H%i')
ALTER TABLE my_table CHANGE `_time` `_time` DATETIME NOT NULL

This works because STR_TO_DATE() makes DATETIME values of your strings, and then MySQL casts them back to strings to store back into your varchar column. Then you can change the datatype to DATETIME.

You probably noticed I threw in NOT NULL. If you're going to put an index on that column, NOT NULL is a good thing to have. But, if some of your time values are missing, it won't work.




回答3:


Because the database doesn't know what to do with 201409201945, it's not a valid DateTime format, therefore it can't change it

You can delete the data that is in it already, and then try changing it



来源:https://stackoverflow.com/questions/25948304/unable-to-convert-varchar-to-datetime-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!