Search code examples
mysqlcoldfusiondate-formatqoq

Coldfusion MySQL Query of Queries date selections


I'm trying to query a query in Coldfusion with a timestamp field (yyyy-mm-dd HH:mm:ss) reformatted to a specific date. I can get the original query to "date format" fine but I can't get the query of query to select the date. I have tried changing the cf_sql queryparam to different values but I keep getting a 0 RecordCount. The db field is a varchar and I tried changing that to a timestamp and it still didn't work. Any ideas what I'm not seeing?

<cfquery datasource="#DataSource#" name="rsAll">
  SELECT ID, date_format(DateSent, '%Y-%m-%d') As DateSent 
  FROM 'workorders'
  WHERE RelCompID_FK = '#SESSION.Auth.CompID#' 
</cfquery>

<cfquery name="rsAllDay" dbtype="query">
  SELECT *
  FROM rsAll
  WHERE  DateSent  = <cfqueryparam value="2010-03-03" cfsqltype="cf_sql_date">
</cfquery>

Solution

  • The MySQL date_format() function returns a string, not a datetime object, so when running your query of query in ColdFusion you would use the CF_SQL_VARCHAR datatype instead of the CF_SQL_DATE datatype. The value passed in would need to exactly match the value returned from the first query as well to match.

    Other considerations:

    1) Use CFDUMP to display the results of the first query to ensure that the data types returned are what you're expecting.

    2) Use CFQUERYPARAM in the WHERE clause of your first query as well.