Around the web
« The discHub | Main | No more IE updates for older Windows »
Friday
Sep242004

ColdFusion QoQ Date Comparison

ColdFusion Query of Queries are a useful way of filtering an existing, in-memory recordset by executing a SQL query against the dataset.



Unfortunately, the documentation and more importantly, error handling for this otherwise cool ColdFusion feature are a bit lacking. Today I was trying to filter a query by date. The query had columns such as:



articleid,headline,author,publishdate



I needed to get the articles after a certain date and tried to do a simple QoQ:



<cfquery dbtype="query" name="filter">

select * from results WHERE publishdate > #startDate#

</cfquery>



This query returns an error about unsupport type comparision. After trying numerous functions and syntax changes, Ryan suggested I try CFQUERYPARAM, which (unexpectedly) worked perfectly:



<cfquery dbtype="query" name="filter">

select * from results

WHERE publishdate > <cfqueryparam value="#startDate#" cfsqltype="CF_SQL_DATE">

</cfquery>

This got me the exact results I needed. I'm still not exactly sure why the CFQUERYPARAM fixed the problem; my impression was that it was used for doing parameterized queries (prepared statements) against a SQL database, but in this case it appears to be setting some sort of data type flag internally that allows the comparison to work.



If you've been working with ColdFusion for a long time, you may be wondering why I just don't filter the original query (results) rather than use the slower QoQ functionality. In this case though, the query is not coming from a database and doesn't support date filtering - so QoQ is exactly the tool needed to do the filtering.

Reader Comments (8)

Yeah, i'm big in Japan.

September 24, 2004 | Unregistered CommenterRyan

Nice work. Wish Macromedia would include this example in their documentation.

http://www.angrysam.com" REL="nofollow">David Levin

March 5, 2006 | Unregistered CommenterDave

I try to do QoQ with verity search...

I can't get it...

what format has your startdate

made it with dateAdd

June 23, 2006 | Unregistered CommenterAnonymous

Wow... that was just the answer I was looking for. Thanks!

October 3, 2006 | Unregistered CommenterTim

Thank you soo much. I spent an hour trying to figure this out and your comments saved the day. Most appreciated. It worked beautifully.

October 17, 2006 | Unregistered CommenterAnonymous

big THANKS!

February 27, 2007 | Unregistered CommenterAnonymous

Well, this does get past the unsupported type comparison, but it still seems to evaluating the data as a string (and filtering alphabetically) and not as a date (and filtering chronologically). Ben Nadel (of course) shares the workaround: http://www.bennadel.com/blog/379-ColdFusion-Query-of-Queries-Unexpected-Data-Type-Conversion.htm

June 11, 2009 | Unregistered Commenterphenotypical

thanks a lot :)

October 14, 2009 | Unregistered CommenterDeepak Yadav

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>