I would like to update the whole column from the format of yearmonthday to just monthday
for example a value in this column is 20130401 and I want to change it to 0401
I would like to update the whole column from the format of yearmonthday to just monthday
for example a value in this column is 20130401 and I want to change it to 0401
如果您的列是 varchar 列,则可以使用如下内容:
UPDATE yourtable
SET
col = DATE_FORMAT(STR_TO_DATE(col, "%Y%m%d"), "%m%d")
或者也许只是:
UPDATE yourtable
SET col = RIGHT(col, 4)
编辑
如果您需要先创建一个新列然后删除旧列,您可以使用这个:
ALTER TABLE yourtable ADD COLUMN col2 VARCHAR(20);
UPDATE yourtable
SET col2 = RIGHT(col, 4);
ALTER TABLE yourtable DROP COLUMN col;
请在此处查看小提琴。