How to Change the datatype of columns in MYSQL table

preview_player
Показать описание
SQL Query

Alter Table [tablename] Modify column [columnName] [datatype];
ALTER TABLE [tablename] CHANGE [columnName] [columnName] [datatype];

-- Error Code: 1292. Incorrect date value: '01/01/2011' for column 'order date' at row 1'
-- Replace "-" with "/"
select cast(replace(`order date`,'-','/') as date) from amazingmart;
-- str_to_date , date_format
update amazingmart set `order date` = replace(`order date`,'-','/') ;
ALTER TABLE amazingmart CHANGE column `order date` `Order_Date` varchar(20);
update amazingmart set order_date = date_format(str_to_date(order_date,'%d/%m/%Y'),'%Y-%m-%d');
select cast(Order_Date as date) from amazingmart;
ALTER TABLE amazingmart Modify column `Order_Date` date;
Рекомендации по теме
Комментарии
Автор

Your explanation is very clear and helpful for beginners

sunitasankurathri
Автор

Thank you for this video. It's very helpful. Keep it up!!

COD_MW
Автор

Thank you so much. That was really helpful
Is there any way we can use one or two line of code to achieve the same result of changing date type from TEXT to DATE type?

Once again, thank you.

sharonnwanne