Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting empty mysql datetime fields

Is there a better way to select empty datetime fields than this?

SELECT * FROM `table` WHERE `datetime_field` = '0000-00-00 00:00:00' 
like image 414
Callum Avatar asked Feb 18 '09 02:02

Callum


People also ask

Can datetime be NULL MySQL?

MySQL DOES accept null values for the datetime definition, but if you for some reason think otherwise and won't use a null value, consider simply using '1000-01-01' as the value and excluding rows which have that value for your bill_date column in your queries. Mysql does allow nulls in datetime fields.

How do I select only NULL values in MySQL?

To look for NULL values, you must use the IS NULL test. The following statements show how to find the NULL phone number and the empty phone number: mysql> SELECT * FROM my_table WHERE phone IS NULL; mysql> SELECT * FROM my_table WHERE phone = ''; See Section 3.3.

How do I check if a field is empty in MySQL?

SELECT * FROM yourTableName WHERE yourSpecificColumnName IS NULL OR yourSpecificColumnName = ' '; The IS NULL constraint can be used whenever the column is empty and the symbol ( ' ') is used when there is empty value.

How do you handle NULL values in a date column?

Try using the function IS_SPACES(). If your source also has NULL values then use IS_NULL() function and route the data accordingly. Adding to what Pallavi and Prashant mentioned, there are two actions you will need to do, Identify the NULLS and replace it with an absolute date value or NULL.


1 Answers

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:

SELECT * FROM `table` WHERE `datetime_field` IS NULL 

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 NULL may not work.

like image 177
paxdiablo Avatar answered Oct 10 '22 07:10

paxdiablo