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>
The MySQL
date_format()function returns a string, not a datetime object, so when running your query of query in ColdFusion you would use theCF_SQL_VARCHARdatatype instead of theCF_SQL_DATEdatatype. The value passed in would need to exactly match the value returned from the first query as well to match.Other considerations:
1) Use
CFDUMPto display the results of the first query to ensure that the data types returned are what you’re expecting.2) Use
CFQUERYPARAMin the WHERE clause of your first query as well.