Jump to content

Rearranging my DB. (Simple question for some…)


DBookatay

Recommended Posts

I first created my database several years ago, and have since been trying to make changes to it that makes it "proper."

I have 3 char(2) rows: sold_date1, sold_date2 and sold_date3, with values such as "10", "12", "09".

 

How do I take the values from those rows and convert and update them so that they get inserted into a new row "sold_date" with the "date" type?

 

Example "2009-10-12"

 

You would form a single UPDATE query * that uses the mysql CONCAT() function to take the three existing column values and produce the value that you set the new column to.

 

* An update query without a WHERE clause will match all the rows in your table and update every row by executing the query once.

 

The query would look something like (test it first on a sample of your data to be sure) -

UPDATE your_table SET sold_date = CONCAT('20',sold_date3,'-',sold_date1,'-',sold_date2);

 

2 follow up questions, and forgive me because I'm still learning.

    1. This database was started in 1998, so how do I add

CONCAT('19',sold_date3,'-',sold_date1,'-',sold_date2);

if it's a 1998 or 1999 and then a

CONCAT('20',sold_date3,'-',sold_date1,'-',sold_date2);

if it's year 2000 and up.

    2. If there already is the proper date in the "sold_date" field, how do I exclude those. Meaning I only want to alter the rows that have "sold_date" as "0000-00-00" AND data in the "sold_date1", "sold_date2" and "sold_date3" fields.

Untested, but should work -

UPDATE your_table SET sold_date = CONCAT(IF(sold_date3 IN ('98','99'),'19','20'),sold_date3,'-',sold_date1,'-',sold_date2) WHERE sold_date != '0000-00-00' AND sold_date1 !='' AND sold_date2 != '' AND sold_date3 != '';

 

Edit: I just tried that query and the sold_date != '0000-00-00' logic is backwards. It should be sold_date = '0000-00-00'

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.