alan93 Posted October 10, 2008 Share Posted October 10, 2008 I have a date field in records that is formatted as such: 1/1/2009 or 4/5/2007 This field that holds these values is a varchar(16) I need to delete all records that has a date older than todays date. How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/127904-need-date-comparison-and-delete-record-query/ Share on other sites More sharing options...
ainoy31 Posted October 10, 2008 Share Posted October 10, 2008 I would change the field to a 'date' data type rather than varchar(16). Try this code out: select * from table_name WHERE date_column <= CURDATE() or select * from table WHERE date_column >= CURDATE() In your case you would use Delete but try the Select first to make sure that it returns the records you need. Quote Link to comment https://forums.phpfreaks.com/topic/127904-need-date-comparison-and-delete-record-query/#findComment-662182 Share on other sites More sharing options...
discomatt Posted October 10, 2008 Share Posted October 10, 2008 I have a date field in records that is formatted as such: 1/1/2009 or 4/5/2007 This field that holds these values is a varchar(16) I need to delete all records that has a date older than todays date. How do I do that? Good luck doing it with VARCHAR. Shoudla done with a date type column. Quote Link to comment https://forums.phpfreaks.com/topic/127904-need-date-comparison-and-delete-record-query/#findComment-662189 Share on other sites More sharing options...
Maq Posted October 10, 2008 Share Posted October 10, 2008 If I were you I would create a new field with date_time datatype and write a script to go through your current varchar dates and convert them... It will make things easier for this situation and future problems. Quote Link to comment https://forums.phpfreaks.com/topic/127904-need-date-comparison-and-delete-record-query/#findComment-662243 Share on other sites More sharing options...
PFMaBiSmAd Posted October 10, 2008 Share Posted October 10, 2008 After you add a DATE type column, a single UPDATE query using the mysql STR_TO_DATE() function can populate the new column from the existing data. Quote Link to comment https://forums.phpfreaks.com/topic/127904-need-date-comparison-and-delete-record-query/#findComment-662252 Share on other sites More sharing options...
alan93 Posted October 11, 2008 Author Share Posted October 11, 2008 I didn't know you could change the type after the column was populated. I guess I could try that. Hope it doesn't break anything. [ quote author=ainoy31 link=topic=220469.msg1011351#msg1011351 date=1223666580] I would change the field to a 'date' data type rather than varchar(16). Try this code out: select * from table_name WHERE date_column <= CURDATE() or select * from table WHERE date_column >= CURDATE() In your case you would use Delete but try the Select first to make sure that it returns the records you need. Quote Link to comment https://forums.phpfreaks.com/topic/127904-need-date-comparison-and-delete-record-query/#findComment-662778 Share on other sites More sharing options...
Maq Posted October 11, 2008 Share Posted October 11, 2008 I think he meant create a new column with the date type use the code to populate it, delete ur old column and rename the new one. Quote Link to comment https://forums.phpfreaks.com/topic/127904-need-date-comparison-and-delete-record-query/#findComment-662790 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.