Solved

varchar to datetime mysql

Posted on 2011-09-20
7
739 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
  • 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now