Solved

MySQL Alter Table

Posted on 2013-11-08
5
459 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
  • 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Creating and Managing Databases with phpMyAdmin in cPanel.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

861 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

22 Experts available now in Live!

Get 1:1 Help Now