Solved

varchar to datetime mysql

Posted on 2011-09-20
7
745 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 500 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

695 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