Bug #10391 SELECT MAX(a_datetime_field) returns a String rather than a Timestamp
Submitted: 5 May 2005 14:56 Modified: 5 May 2005 15:03
Reporter: Richard Wood Email Updates:
Status: Duplicate Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:MySQL Server 4.1 OS:Windows (Window XP)
Assigned to: CPU Architecture:Any

[5 May 2005 14:56] Richard Wood
Description:
A JDBC query with a GROUP BY clause that SELECTs MAX or MIN on a MySQL datetime field returns a String rather than the expected Timestamp.  A JDBC query with NO GROUP BY clause that SELECTs MAX or MIN on a MySQL datetime field returns the expected Timestamp.  

How to repeat:
Using Java 1.4.2 and mysql-connector-java-3.1.8a or 3.0.15, Query a MySql table "sometable" containing a datetime "datetime1" field and some other field "otherfield" to which a group by clause can be applied, to return a result set.

Perform the following Java JDBC query:
String sql = "SELECT datetime1 FROM sometable";
ResultSet rs = mystatement.executeQuery(sql);

Check the object type of the column returned for the datetime field:
rs.next();
String class = rs.getObject("datetime1").getClass().getName();
class = "Timestamp" as expected

Now add a MAX aggregate to the query
sql = "SELECT MAX(datetime1) FROM sometable";
rs = mystatement.executeQuery(sql);
rs.next();
class = rs.getObject("datetime1").getClass().getName();
class = "Timestamp" as expected

Now add a GROUP BY clause to the query
sql = "SELECT MAX(datetime1) FROM sometable GROUP BY otherfield";
rs = mystatement.executeQuery(sql);
rs.next();
class = rs.getObject("datetime1").getClass().getName();
class = "String", NOT "Timestamp"

Suggested fix:
The statement with the group by clause should return a Timestamp, NOT a String
[5 May 2005 15:03] MySQL Verification Team
Duplicate: http://bugs.mysql.com/bug.php?id=10390