views:

810

answers:

3

Is there solution for '0000-00-00 00:00:00' problem, without changing table?

I have "[]" in this query:

dbh.select_all("select j.n, j.name, j.dsc, j.flag, j.td from job j where j.td='0000-00-00 00:00:00'")

I look solution for this: http://rubyforge.org/tracker/index.php?func=detail&aid=22243&group_id=234&atid=967

A: 

I usually declare my datetimes as accepting NULL, e.g.:

dtime DATETIME NULL DEFAULT NULL

That way I can check if a field IS NULL rather than if a field = '0000-00-00 00:00:00'.

Peter
I can't change table, there many people who works with it
amirka
+1  A: 

So your default is '0000-00-00 00:00:00' and you can't change it. I dug up my ruby-dbi mailing list archives and found the following explanation:

The problem with the latter case is that it can't be coerced to a DateTime object because it's not a valid time... Honestly, I'm surprised mysql allows it at all. Either way, you'll need to turn type conversion off (see DBI.convert_types=) to get this default value to work or change it and all occurrences of it in your database, or use bind_coltype to treat it as string instead.

See this mailing list archive.

Sarah Mei
thanks, DBI.convert_types = falsesolve problem
amirka
A: 

You can just cast the "0000-00-00 00:00:00" to NULL:

SELECT IF(mytime="0000-00-00 00:00:00",NULL,mytime) FROM mytable;

Michael Andrews