MySQL更改表并将数据从文本转换为日期时间 [英] MySQL alter table and convert data from text to datetime
本文介绍了MySQL更改表并将数据从文本转换为日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有列日期的表,但它存储为文本。
I have a table with a column date but it is stored as text.
现在我需要根据日期提取数据,我想我需要将列类型更改为datetime,但是如何做到这一点而又不丢失数据呢?我的文本记录的格式为dd-MM-YYYY hh:mm
Now I need to extract data based on date and I'm thinking I need to alter the column type to datetime, but how can I do that without losing data? My text records are in format dd-MM-YYYY hh:mm
如果仅更改列类型,则数据将丢失所有数据(用零填充)。
If I just change the column type the data I lose all data (it is filled with zeros).
推荐答案
UPDATE `table`
SET `column` = STR_TO_DATE(`column`,'%d-%M-%Y %h:%i')
只需更改格式,以防不正确。
Just change the format to what you have,in case that is not correct.
这篇关于MySQL更改表并将数据从文本转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文