You're using the correct function STR_TO_DATE(str,format)
to achieve the goal, but you're making two mistakes:
- In your query the
format
argument does not match the string expression format. You said it's in dd-mm-yy
format while you passed %Y,%m,%d
(comma separated) to the format
argument. The query will return a "incorrect datetime value" error. You should use %d-%m-%Y
.
- You can't change data type of a column on the fly, by setting different type of the value being passed. You have to first update the values and then change data type for column.
So, summarizing:
mysql_query("UPDATE `Table` SET `date` = STR_TO_DATE(`date`, '%d-%m-%Y')");
mysql_query("ALTER TABLE `Table` CHANGE COLUMN `date` `date` DATE");
Additionally, consider switching to the recommended PDO extension in place of old and slowly deprecated mysql
extension.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…