Converting Date (d-m-Y_His) stored in mysql VARCHAR to mysql DATETIME

Hi,
I was trying to sort records in one of our MySQL databases by date but I notice that the date values are stored as VARCHAR data type rather than a DATETIME value.

i.e.  08-07-2016_164533     in the format d-m-Y_His  (day-month-Year_HourMinuteSecond)

I wonder if there is an easy way to convert these values to DATETIME datatype for easier manipulation?

Thanks
EICTAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
There is not an easy way to do it with a single command. It is a multiple step process.
1. Create a new column in your table:
ALTER TABLE tab ADD COLUMN col2 DATETIME;

2. Update the new column from the old one:
UPDATE tab SET col2 = STR_TO_DATE(col1, '%d-%m-%Y_%k%i%S');

3. Validate
SELECT col1, col2 FROM tab;

4. Drop the old column:
ALTER TABLE tab DROP COLUMN col1;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skullnobrainsCommented:
agreed except i'd use

1. lock table
2. create table table_tmp as select .... , STR_TO_DATE(col1, '%d-%m-%Y_%k%i%S') from table
3. rename table to table_old , table_tmp to table
4. drop table table_old

which will copy the whole table a single time rather than twice
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.