MySQL DATE_FORMAT() Returning Binary Data

Taming the MySQL Date_Format Function

MySQL LogoThis is just a quick post of a little problem I've been running into lately. I'm not sure if it's a bug or some kind of intended MySQL thing, but it's annoying as heck. Also, I'm using MySQL version 5.0.32. I'm not sure if this applies to older versions of MySQL or not, but it seems to be something new with the 5.0 variants.

The problem stems from working with date objects and trying to format them using DATE_FORMAT(). Let's assume we have a database table that has two datetimes, '2009-12-12 15:30:35' and '2009-12-12 23:03:11'. We want to format the dates as MONTH-YEAR. Our query might look something like this:

<cfquery name="getDates" datasource="myDSN">
   SELECT DATE_FORMAT(my_date,"%m-%Y") as my_formated_dates
   FROM my_table
   ORDER BY my_date DESC
</cfquery>

If I do a CFDUMP of the data, I get:

+-------------------|
| my_formated_dates |
+-------------------+
| 1. binary         |
| 49504750484857    |
+-------------------+
| 2: binary         |
| 49504750484857    |
+-------------------+

See what it's doing there, it's treating my datetime objects as BINARY data. Again, I'm not sure if this is a bug or if it's meant to act that way, but for whatever reason the DATE_FORMAT() function is not returning a STRING object like you would expects. I can't even speculate what's going on, it's either the DATE_FORMAT function or the DATE object that's gone all wacko on us.

The good news, is that I did come across a fix. It's fairly simple really, we just need to convert the result to a STRING. Like so:

<cfquery name="getDates" datasource="myDSN">
   SELECT CONVERT(DATE_FORMAT(my_date, '%m-%Y') USING latin1) AS my_formated_dates
   FROM my_table
   ORDER BY my_date DESC
</cfquery>

And now if we do a CFDUMP we get what we would expect:

+-------------------|
| my_formated_dates |
+-------------------+
| 1. 12-2009        |
+-------------------+
| 2: 12-2009        |
+-------------------+

Hope this helps anyone else experiencing this problem. And if you have any insight to whats going on, please share!

1 responses to "MySQL DATE_FORMAT() Returning Binary Data"

CharlyMX
In Oracle 10g, you need to do that to print a date in any format, like you want to do in MySQL... You you didn't know that in MySQL.
Doug
Doug responds:
@CharlyMX - I appreciate the insight. I have never claimed that I was a SQL expert, so some of these things baffle me at times.

Thanks!

Have something to say? Leave a comment!

You don't have to be registered to leave a comment. Unregistered user's comments will be approved before going live.




You are currently posting as an unregistered user.
This means that your comment will be reviewed prior to going live. If you are a registered user, please . New user? No problem, register for an account, it's FREE! Benefits include, posting instantly, screen name protection, collaboration recognition, subscribe to article updates, and so much more!