Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting MySQL datetime columns which are 0000-00-00 00:00:00

I'm looking for a simple way to select datetime columns which are 000-00-00 00:00:00 (basically the default unset value).

The following query seems to be working:

SELECT * FROM myTable WHERE datetimeCol < 1

But will this reliably work all the time and across different mysql versions?

like image 975
Ali Avatar asked Dec 01 '22 20:12

Ali


2 Answers

I would check for "zero" dates like this:

SELECT * FROM myTable WHERE datetimeCol = CONVERT(0,DATETIME)

I would prefer the equality predicate over an inequality predicate; it seems to convey my intentions more clearly. And if there are more predicates in the statement, and if the datetimeCol is a leading column in an index, it may help the optimizer make use of a multi-column index.

like image 154
spencer7593 Avatar answered Dec 21 '22 13:12

spencer7593


Try this:

SELECT * FROM myTable WHERE datetimeCol = '0000-00-00 00:00:00'

Might seem obvious in hindsight ;)

like image 32
Dan Breen Avatar answered Dec 21 '22 12:12

Dan Breen