Solved

MySQL Alter Table

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
SELECT query on two levels (detail and summary) 13 69
MySQL recovery 7 57
php hashing methods 3 48
MySQL_Development_Traininng.. 10 14
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 …
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

732 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