Is there a better way to select empty datetime fields than this?
SELECT * FROM `table` WHERE `datetime_field` = '0000-00-00 00:00:00'
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Better in what way? That query does everything you ask of it and, provided there’s an index on
datetime_field, it’s as fast as it’s going to get.If you’re worried about the query looking ‘ugly’, don’t be. Its intent is quite clear.
The only possible improvement you could consider is to use NULLs for these zero-date/time rows, in which case your query becomes:
That’s what I’d do with a standards-compliant DBMS. My understanding is that MySQL may represent true NULL datetime fields in this horrific manner, in which case I guess the
IS NULLmay not work.