Switch VARCHAR to DATE in MySQL

Use str_to_date() to handle the conversion.

str_to_date('2016-12-4', '%Y-%m-%d'); 

SELECT CAST('2016-12-4' AS DATE); --> 2016-12-04, so you don't need str_to_date.

I would add a new column for the date (ALTER TABLE .. ADD COLUMN ..); UPDATE to set the new column. Then manually fix any really messed up values. Finally DROP COLUMN and RENAME COLUMN;

ORDER BY a VARCHAR that contains a date in it may lead wrong answers. (Jan and Nov may get confused.) Testing for specific dates may fail. (Missing leading 0.)

And indexing the DATE column will make certain queries faster.


Changing the VARCHAR column to an DATE column is also not a problem.

Test table and data

CREATE TABLE test.t1( `date_start` VARCHAR(20) ); 
INSERT INTO test.test1 VALUES('2016-1-5');
INSERT INTO test.test1 VALUES('2016-1-5');
INSERT INTO test.test1 VALUES('2016-12-5');
INSERT INTO test.test1 VALUES('2016-12-6');

Select query result.

SELECT * FROM test.test1

date_start  
------------
2016-1-5    
2016-1-5    
2016-12-5   
2016-12-6   

Change the column VARCHAR to DATE

ALTER TABLE test.test1 CHANGE date_start date_start DATE NULL;

Select query result.

SELECT * FROM test.test1 

date_start  
------------
2016-01-05  
2016-01-05  
2016-12-05  
2016-12-06