Disclaimer: this is needed for an old and ugly codebase that I’d much rather not touch.
- There is a table in the database that has a column (among many others) of
DATEtype. - There is a query (auto generated by Torque criteria – don’t ask) with
java.util.Dateparameter; Torque formats it as full datetime.
So, the query MySQL is getting is something like:
SELECT * FROM my_table
WHERE my_date = '20091201105320'; // 2009-12-01 10:53:20
The problem is that on one server which has MySQL 5.0.27-standard it works just fine – as in returns all the records from my_table that have my_date set to ‘2009-12-01’. On another server which has MySQL 5.0.45 it does not work. The data is there – if I manually run the query after trimming the time portion out I get correct results back.
The question is:
Is there a setting somewhere, either in MySQL configuration file or in per-session variables (connection string) or somewhere else to force MySQL to ignore the time portion during string constant to date conversion?
I obviously can’t use DATE() function without changing the codebase which I’d much rather not.
Apparently I’m out of luck. From MySQL manual:
Prior to MySQL 5.0.42, when
DATEvalues are compared withDATETIMEvalues, the time portion of theDATETIMEvalue is ignored, or the comparison could be performed as a string compare. Starting from MySQL5.0.42, a
DATEvalue is coerced to theDATETIMEtype by adding the time portion as'00:00:00'.Should anyone find themselves in a similar situation, the only workaround I found was to:
java.util.Dateinstance to zero.getDateString()method of Torque’s DB adapter for MySQL (org.apache.torque.adapter.DBMM) to check whether time fields of a date are zeroes and, if so, use “date-only” format.