?
Solved

MySQL Alter Table

Posted on 2013-11-08
5
Medium Priority
?
468 Views
Last Modified: 2013-11-10
I have a MySQL table i inherited, there is a column with a datatype of varchar(50) all the values are dates.

When I try to change the data type to Date and apply I am getting the following message

ERROR 1292: Incorrect date value: '12/01/1943' for column 'DOB' at row 1

I have another column in the table set as date and it returns in this format
2011-07-20

This looks like the problem, any quick fix to this?
0
Comment
Question by:Brogrim
[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
  • 3
  • 2
5 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39634533
You'll need to make sure all the VarChar dates are in a format that will easily convert to DateTime format - YYYY-MM-DD

If all the VarChar dates are in a particular format (such as DD/MM/YYYY), then you can write a query to change that around, and update the field so it's in the correct format.

Then you'd be able to change your Data Type easily
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 39634570
You'll need to run a query like this before altering your table:

UPDATE yourTable SET DOB = DATE_FORMAT(STR_TO_DATE(DOB, '%d/%m/%Y'), '%Y-%m-%d');

Open in new window

This will convert the VarChar date from dd/mm/yyyy format to the yyyy-mm-dd format. Once that's done, you can alter your table and change the column datatype to date.

If you dates are stored in mm/dd/yyyy format (difficult to tell from 12/01/1943!) then just change the STR_TO_DATE function to this:

STR_TO_DATE(DOB, '%m/%d/%Y')
0
 

Author Comment

by:Brogrim
ID: 39634917
Thanks for the suggestion and no doubt it would work if all the dates were in the format dd-mm-yy, unfortunately the column been varchar (50) there were multiple instances like

002/1/1929
1/122/201
 and so on

Below is the message I got back

Error Code: 1292. Truncated incorrect date value: '6/6/12922'


What I have ended up doing is writing a statement

select id, dob, CONCAT(right(DOB,4),+-+ MID(DOB, 4,2),+-+ left(DOB,2)) as NEWDOB from tbl where dob is not null

Open in new window


I started off with 41,000 records and ended up with 40,200 as alot could'nt be salvaged

I did have to run the query multiple times before I could then create another table, I then added a new column DOB1 and updated DOB1 through the ID join on both tables.

Long way around it but its working.
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39634982
Yeah - no easy way to do it if your varchar column contains garbage - bad database design from the off, I think :(

Pleased you got it working
0
 

Author Closing Comment

by:Brogrim
ID: 39636886
Thanks Chris, the comment you made was correct and would have worked but for the initial bad design.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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