sleepyw Posted May 29, 2009 Share Posted May 29, 2009 Total noob question. My understanding is that, by default, a date field in MySQL will populate 0000-00-00 if nothing is entered. My question is, can I change that in MySQL to leave that area blank if there is no value? If not, is there an easy way to code PHP to not show a 0000-00-00 without writing an if statement for every date field where this applies? TIA. Link to comment https://forums.phpfreaks.com/topic/160143-solved-how-to-remove-or-not-display-the-0000-00-00-in-date-fields/ Share on other sites More sharing options...
JonnoTheDev Posted May 29, 2009 Share Posted May 29, 2009 Yes, you should default it to NULL. Make sure the field can contain a NULL. Only defaults to 0000-00-00 if you insert an empty value or an invalid date <?php $created = ""; mysql_query("INSERT INTO table SET created=".(strlen($created) ? "'".$created."'" : "NULL").""); ?> Link to comment https://forums.phpfreaks.com/topic/160143-solved-how-to-remove-or-not-display-the-0000-00-00-in-date-fields/#findComment-844932 Share on other sites More sharing options...
sleepyw Posted May 29, 2009 Author Share Posted May 29, 2009 EDIT: to test, I erased the 000-00-00 value in a few fields and set to NULL and it appears to work. However, I would need/want to run UPDATE command to make the change to the db globally. Something like: UPDATE table SET %all fields that apply% = 'NULL' WHERE %all fields that apply% = '0000-00-00' The text between the %'s would need to be a catch-all for the whole db. Is that possible? I don't understand what your code does. Link to comment https://forums.phpfreaks.com/topic/160143-solved-how-to-remove-or-not-display-the-0000-00-00-in-date-fields/#findComment-845168 Share on other sites More sharing options...
Ken2k7 Posted May 29, 2009 Share Posted May 29, 2009 UPDATE tablename SET column1_name = 'NULL', column2_name = 'NULL'; For more columns, put a comma and set it to NULL. Link to comment https://forums.phpfreaks.com/topic/160143-solved-how-to-remove-or-not-display-the-0000-00-00-in-date-fields/#findComment-845188 Share on other sites More sharing options...
sleepyw Posted May 29, 2009 Author Share Posted May 29, 2009 So there's not a "catch-all" to just replace all instances of '0000-00-00' with 'NULL', regardless of the field name? Link to comment https://forums.phpfreaks.com/topic/160143-solved-how-to-remove-or-not-display-the-0000-00-00-in-date-fields/#findComment-845194 Share on other sites More sharing options...
JonnoTheDev Posted May 29, 2009 Share Posted May 29, 2009 yes, like you put before UPDATE table SET whatever = NULL WHERE whatever = '0000-00-00' Link to comment https://forums.phpfreaks.com/topic/160143-solved-how-to-remove-or-not-display-the-0000-00-00-in-date-fields/#findComment-845197 Share on other sites More sharing options...
sleepyw Posted May 29, 2009 Author Share Posted May 29, 2009 No - I'm saying instead of listing out every column name where it might apply, how do I do a global"search and replace" from 0000-00-00 to NULL? I can't use the field name "whatever". Could I use SET * = 'NULL' WHERE * = '0000-00-00'? Link to comment https://forums.phpfreaks.com/topic/160143-solved-how-to-remove-or-not-display-the-0000-00-00-in-date-fields/#findComment-845201 Share on other sites More sharing options...
Ken2k7 Posted May 29, 2009 Share Posted May 29, 2009 No you can't. Link to comment https://forums.phpfreaks.com/topic/160143-solved-how-to-remove-or-not-display-the-0000-00-00-in-date-fields/#findComment-845203 Share on other sites More sharing options...
sleepyw Posted May 29, 2009 Author Share Posted May 29, 2009 Well, bugger! OK, thanks for the help guys. I guess I'll list out all the columns and try that. Link to comment https://forums.phpfreaks.com/topic/160143-solved-how-to-remove-or-not-display-the-0000-00-00-in-date-fields/#findComment-845214 Share on other sites More sharing options...
JonnoTheDev Posted May 30, 2009 Share Posted May 30, 2009 ah, I see Sorry no Link to comment https://forums.phpfreaks.com/topic/160143-solved-how-to-remove-or-not-display-the-0000-00-00-in-date-fields/#findComment-845650 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.