Solved

MySQL Alter Table

Posted on 2013-11-08
5
463 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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