Solved

varchar to datetime mysql

Posted on 2011-09-20
7
743 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 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