Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

varchar to datetime mysql

Posted on 2011-09-20
7
Medium Priority
?
751 Views
Last Modified: 2012-05-12
hi,

i have huge db in which unknowingly i kept the column as varchar which actually has to be datetime. The data stored in it is in '16/September/2011' format.
Now i want to convert the data type to datetime without losing the data.
ie 2011-09-11 rest part can be ignored
is it possible?

db : mysql
programing language : php
0
Comment
Question by:Insoftservice
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 24

Expert Comment

by:johanntagle
ID: 36566984
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date

in your case it should be STR_TO_DATE('16/September/2011','%d/%M/%Y')

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36566992
Here's what I'd do.

1. Insert a new column.
2. Modify any software you have that uses the old field and change it to use the new field.  Hopefully you'll find everything.
3. Write a PHP program to read from the old column and write to the new column.  
3a. If the new column has data, I wouldn't overwrite it unless you are interested in the most recent date in which case, I'd overwrite it if not the most recent.  Why?  In case you don't find all instances that use the old field in step #2.

4. If you are absolutely certain that you don't have any software remaining that uses the old column (and you are absolutely certain you'll never need to revert to a prior version) then you can delete the old column.  Me, I'd keep the old column around for quite some time.
0
 
LVL 24

Accepted Solution

by:
johanntagle earned 2000 total points
ID: 36566998
To complete the procedure:

1.  create a new date column
2.  run: update table_name set date_column= STR_TO_DATE(original_column,'%d/%M/%Y');
3.  drop the old column
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:johanntagle
ID: 36567014
Agree with hmccurdy's steps, except you don't need a PHP program for step 3.  To not overwrite on existing data, just do:

update table_name set date_column= STR_TO_DATE(original_column,'%d/%M/%Y') where data_column IS NULL;
0
 
LVL 24

Expert Comment

by:johanntagle
ID: 36567047
data_column --> date_column
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36567061
johan, you are correct.  I just like writing PHP programs.  
0
 
LVL 15

Author Closing Comment

by:Insoftservice
ID: 36567413
hi,

i had that code but with different format and thats the reason why it was not working i did not dropped so as to be in safe hand.
Thanx it worked
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question