DBookatay Posted October 29, 2010 Share Posted October 29, 2010 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" Link to comment https://forums.phpfreaks.com/topic/217162-rearranging-my-db-simple-question-for-some%E2%80%A6/ Share on other sites More sharing options...
coupe-r Posted October 29, 2010 Share Posted October 29, 2010 Im not really sure what you are trying to do but? $newdate = $row['sold_date3'].'-'.$row['sold_date2'].'-'.$row['sold_date1']; year-mon-day Am I close to what you want? Link to comment https://forums.phpfreaks.com/topic/217162-rearranging-my-db-simple-question-for-some%E2%80%A6/#findComment-1127817 Share on other sites More sharing options...
DBookatay Posted October 29, 2010 Author Share Posted October 29, 2010 I want the SQL code that will pull the values from sold_date1, sold_date2, and sold_date3 and put them combined into "sold_date". Then I can delete the rows and content of sold_date1, sold_date2, and sold_date3. Link to comment https://forums.phpfreaks.com/topic/217162-rearranging-my-db-simple-question-for-some%E2%80%A6/#findComment-1127818 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2010 Share Posted October 29, 2010 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. Link to comment https://forums.phpfreaks.com/topic/217162-rearranging-my-db-simple-question-for-some%E2%80%A6/#findComment-1127823 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2010 Share Posted October 29, 2010 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); Link to comment https://forums.phpfreaks.com/topic/217162-rearranging-my-db-simple-question-for-some%E2%80%A6/#findComment-1127824 Share on other sites More sharing options...
DBookatay Posted October 29, 2010 Author Share Posted October 29, 2010 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. Link to comment https://forums.phpfreaks.com/topic/217162-rearranging-my-db-simple-question-for-some%E2%80%A6/#findComment-1128072 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2010 Share Posted October 29, 2010 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' Link to comment https://forums.phpfreaks.com/topic/217162-rearranging-my-db-simple-question-for-some%E2%80%A6/#findComment-1128079 Share on other sites More sharing options...
fenway Posted October 30, 2010 Share Posted October 30, 2010 You can actually use "0" as a shorthand for a zeroed-out date -- comes in handy if you ever change to a DATETIME later. Link to comment https://forums.phpfreaks.com/topic/217162-rearranging-my-db-simple-question-for-some%E2%80%A6/#findComment-1128497 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.