I'm running the following query and QoQ . Could you tell me how should I proceed for the "Download CSV" file option?
<!--- QoQ for FIRSTCONN --->
<cfquery datasource = "XX.XX.X.XX" name="master1">
SELECT STR_TO_DATE(date_format(Timedetail,'%m-%d-%Y'),'%m-%d-%Y') as FIRSTCONN
, COUNT(Timedetail) as FIRSTOccurances
, EVENTS
FROM MyDatabase
WHERE EVENTS = "FIRST"
GROUP BY FIRSTCONN ;
</cfquery>
<!--- Detail Query --->
<cfquery dbtype="query" name="detail1">
SELECT *
FROM master1
WHERE FIRSTCONN >= <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_varchar">
AND FIRSTCONN < <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_varchar">;
</cfquery>
<!--- QoQ for SECONDCONN --->
<cfquery datasource = "XX.XX.X.XX" name="master2">
SELECT STR_TO_DATE(date_format(Timedetail,'%m-%d-%Y'),'%m-%d-%Y') as SECONDCONN
, COUNT(Timedetail) as SECONDOccurances
, EVENTS
FROM MyDatabase
WHERE EVENTS = "SECOND"
GROUP BY SECONDCONN ;
</cfquery>
<cfquery dbtype="query" name="detail2">
SELECT *
FROM master2
WHERE SECONDCONN >= <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_varchar">
AND SECONDCONN < <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_varchar">;
</cfquery>
<cfchart format="flash" chartwidth="1000" chartheight="500" scalefrom="0" scaleto="50000" xAxisTitle="Dates" yaxistitle="Number of Connections">
<cfchartseries query="detail1" type="line" itemColumn="FIRSTCONN" valueColumn="FIRSTOccurances" >
<cfchartseries query="detail2" type="line" itemColumn="SECONDCONN" valueColumn="SECONDOccurances" >
</cfchartseries>
</cfchart>[/CODE]
The cfform code and cfscript code I'm using is as follows:
[CODE]<cfform format="flash" preloader ="false">
<cfformgroup type="horizontal">
<cfinput type="dateField" name="startdate" label="Start Date" width="100" value="#form.startdate#">
<cfinput type="dateField" name="enddate" label="End Date" width="100" value="#form.enddate#">
<cfinput name="submitApply" type="submit" value = "Apply">
<cfinput name="cancel" type="submit" value="Download CSV">
</cfformgroup>
<cfscript>
var tl ='';
var nl = (Chr( 13 ) & Chr( 10 ));
var fileContent = createObject("java","java.lang.StringBuffer").init();
var counter =1;
fileContent.append( 'FIRST');
fileContent.append(nl);
for(i=1;i<=detail1.recordCount;i=i+1){
tl = detail1.FIRST;
fileContent.append(tl);
fileContent.append(nl);
}
fileContent.append( 'SECOND');
fileContent.append(nl);
for(i=1;i<=detail2.recordCount;i=i+1){
tl = detail2.SECOND;
fileContent.append(tl);
fileContent.append(nl);
}
</cfscript>
<cfset absoluteFilePathAndName = " C:\ColdFusion8\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\">
<cfset realtiveFilePathAndName = " C:\ColdFusion8\runtime\servers\coldfusion\SERVER-INF\temp\wwwroot-tmp\">
<cffile action="write" file="#absoluteFilePathAndName#" output="#fileContent.toString()#"/>
<a href="#realtiveFilePathAndName#>Download</a>
Desired Output:
I have attached the for the output below. Please find it attached.
Basically, if a date range is 21June to 21 July. The output must be as shown in the image. (I have omitted THIRDCONN etc for the sake of simplicity in my code).
I tried to attempt to the above problem,Do I need to write fileContent.append() for each and every column? Please let me know if I'm wrong.
P.S. I'm new to CF and haven't done this before.
Thanks
Using separate queries makes this a LOT more difficult than necessary. For it to work, you essentially need to pivot or transpose the rows of each query into separate columns. Not an easy task, unless all of your queries are guaranteed to contain the exact same dates, in the same order (unlikely).
If you have a fixed number of events, it is much simpler to use a single database query to generate all of counts. Use a CASE statements to build the counts based on the "Events" value:
(Note: This cannot be done inside a QoQ. They do not support CASE).
SELECT STR_TO_DATE(date_format(Timedetail,'%m-%d-%Y'),'%m-%d-%Y') as TheDate
, SUM( CASE WHEN EVENTS = 'First' THEN 1 ELSE 0 END ) AS FirstConn
, SUM( CASE WHEN EVENTS = 'Second' THEN 1 ELSE 0 END ) AS SecondConn
, SUM( CASE WHEN EVENTS = 'Third' THEN 1 ELSE 0 END ) AS ThirdConn
FROM YourTableName
WHERE Timedetail >= <cfqueryparam value="#form.startdate#" cfsqltype="cf_sql_date">
AND Timedetail < <cfqueryparam value="#dateAdd('d', 1,form.enddate)#" cfsqltype="cf_sql_date">
GROUP BY STR_TO_DATE(date_format(Timedetail,'%m-%d-%Y'),'%m-%d-%Y')
Once you have all the results in a single query, you can do the rest on your own. As Adam mentioned, just do a search on ColdFusion query to csv. There are tons of examples you can follow, as well as a number of pre-built functions for converting queries to CSV:
CSVFormat()
QueryToCSV()
Related
After I submit a form, I need to check if any field in the database table changed. If changed I create a new record, if not, I don't do anything. I can't just update the record.
Is there a way to do it without checking every single field like the code below?
<cfquery name="getData" datasource="myDS">
Select * From table Where ID = #form.ID#
</cfquery>
<Cfset changed = false />
<!-- Check every field -->
<cfif form.data1 neq getData.data1>
<cfset changed = true />
</cfif>
<cfif form.data2 neq getData.data2>
<cfset changed = true />
</cfif>
<cfif form.data3 neq getData.data3>
<cfset changed = true />
</cfif>
...
Thanks
Might depend on what database you are using but you should be able to do a query that will insert if the data does not exist.
As an example I just tested this against Oracle 12c using CF2016 Enterprise and it creates a new record if the data does not exist.
<cfquery name="Testing" datasource="Test">
INSERT INTO TESTTABLE (DATA1, DATA2, DATA3)
SELECT <cfqueryparam value="#Form.Data1#" cfsqltype="CF_SQL_VARCHAR" />, <cfqueryparam value="#Form.Data2#" cfsqltype="CF_SQL_VARCHAR" />, <cfqueryparam value="#Form.Data3#" cfsqltype="CF_SQL_VARCHAR" />
FROM dual
WHERE NOT EXISTS
(SELECT DATA1, DATA2, DATA3 FROM TESTTABLE WHERE DATA1 = <cfqueryparam value="#Form.Data1#" cfsqltype="CF_SQL_VARCHAR" />,
AND DATA2 = <cfqueryparam value="#Form.Data2#" cfsqltype="CF_SQL_VARCHAR" /> AND DATA3 = <cfqueryparam value="#Form.Data3#" cfsqltype="CF_SQL_VARCHAR" />)
</cfquery>
Can you explain this a little further? Why are you allowing a form to be submitted with changed data if you can't update the record itself? If you click Save, then compare the form data with the query data and have the action call a Create function when the data is different.
Let's say you have a thing query and form:
<cfquery name="myThing">
SELECT
thing_id,
thing_name,
thing_foo
FROM
things
where
thingID = <cfqueryparam value="#url.thingID#">
</cfquery>
<form>
<input type="hidden" name="thing_id" value="#myThing.thing_id#">
<input type="text" name="thing_name" value="#myThing.thing_name#">
<input type="text" name="thing_foo" value="#myThing.thing_foo#">
<button type="submit">Submit</button>
</form>
If you need to check the submitted data against what's already in the database, you can just run the query again on the form processing page and compare those values against the submitted form values. This example assumes you named the form fields the same as the database table columns.
<cfset delta = false>
<cfloop item="key" collection="#myThing#">
<cfif structKeyExists(form, key)>
<cfif form[key] NEQ myThing[key]>
<cfset delta = true>
</cfif>
</cfif>
</cfloop>
If any values differ, then create a new record. No idea what you need to do when the submitted values haven't changed.
<cfif delta>
<!--- Create a new record. --->
<cfelse>
<!--- ¯\_(ツ)_/¯ --->
</cfif>
I've also seen it done where the original values are stored in hidden form fields and submitted along with the editable form fields. You could do this, but there's no guarantee that the values in the DB haven't been changed between you rendering the form and then submitting it.
You'll still have some challenge of how to tell if the DB values have changed on the way to the DB, but I'm not sure if you need so granular a check.
We can use the cfquery to check the table having the existing data or not. If not having the same data means we can Insert the form. The following code may related to your scenario.
<cfif structKeyExists(form,"Submit")>
<cfquery name="checkFormExisting" datasource="myDSN">
SELECT *
FROM USERS
WHERE data1 = <cfqueryparam value="#form.data1#" cfsqltype="cf_sql_varchar">
OR data2 = <cfqueryparam value="#form.data2#" cfsqltype="cf_sql_varchar">
OR data3 = <cfqueryparam value="#form.data3#" cfsqltype="cf_sql_varchar">
</cfquery>
<cfif checkFormExisting.recordCount EQ 0>
INSERT INTO
USERS (
data1,data2,data3
)
VALUES (
<cfqueryparam value="#form.data1#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#form.data2#" cfsqltype="cf_sql_varchar">,
<cfqueryparam value="#form.data3#" cfsqltype="cf_sql_varchar">
)
</cfif>
</cfif>
Hope, you're asking like the above code for your scenario.
I would like to pass the stored procedure result to another stored procedure in ColdFusion. If anyone would be able to help on this.
<cfif not isDefined("getYN")>
<cfstoredproc procedure="stored_proc" datasource="#dsn#">
<cfprocparam cfsqltype="cf_sql_varchar" dbvarname="#lang" type="in" value="#this.lang#"/>
<cfprocparam cfsqltype="cf_sql_varchar" dbvarname="#sqlStatement" type="in" value="#getYN#" null="#NOT len(trim(getYN))#" />
<cfprocresult name="getYN" resultset = "1">
</cfstoredproc>
</cfif>
<cfstoredproc procedure="sp_test" datasource="#dsn#">
<cfprocparam cfsqltype="cf_sql_varchar" dbvarname="#lang" type="in" value="#this.lang#"/>
<cfprocparam cfsqltype="cf_sql_varchar" dbvarname="#sqlStatement" type="in" value="#getYN#" null="#NOT len(trim(getYN))#" />
<cfprocresult name="get" resultset = "2">
</cfstoredproc>
The above is the code example. In the second stored procedure, I am passing the result of 1st stored procedure to the dbvarname sqlStatement of 2nd stored procedure. But the passed value #getYN# should be query instead of result because I am using it for FROM clause.
The 2nd stored procedure in SQL Server is like below:
ALTER PROCEDURE [dbo].[sp_test]
#lang CHAR(5),
#code VARCHAR(20),
#sqlStatement NVARCHAR(MAX) = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sSQL nVARCHAR(max)
SET #sSQL = ' SELECT col1
FROM '+ #sqlStatement +
' WHERE col2 = #lang
AND col3 = #code '
EXECUTE SP_EXECUTESQL #sSQL, N'#lang CHAR(5),
#code VARCHAR(20)', #lang, #code ;
SET NOCOUNT OFF;
END
In addition, the above two code is created from the below code to replace it with cfstoredproc instead of cfquery:
<cfif NOT isDefined("request.getYN")>
<cfquery name="request.getYN" datasource="#request.dsn.pqr#">
SELECT
LANGUAGE_CODE ,
YN_CODE ,
YN_DESCRIPTION
FROM
LANGUAGE_ALTS_YN
WHERE
language_code IN (
'EN','#this.lang#'
)
</cfquery>
</cfif>
<cfquery name="get" dbtype="query">
SELECT
yn_description
FROM
request.getYN
WHERE
language_code =
<cfqueryparam cfsqltype="cf_sql_varchar" value="#this.lang#" />
AND yn_code = <cfqueryparam cfsqltype="cf_sql_varchar"
value="#arguments.code#" />
</cfquery>
The second query really isn't a query. It can't be made into a stored procedure because it does not run on the database server. In other words dbtype="query" is not on the DB server
Besides, you can just filter this data down.
Was
<cfquery name="get" dbtype="query">
SELECT yn_description
FROM
request.getYN
WHERE
language_code = <cfqueryparam cfsqltype="cf_sql_varchar" value="#this.lang#" />
AND yn_code = <cfqueryparam cfsqltype="cf_sql_varchar"
value="#arguments.code#" />
</cfquery>
Should be
get = request.getYN.filter(function () {
return (lang_code == this.lang && yn_code == code);
});
Note: that code on my second line is not scoped. That is not a mistake.
For query filters see: https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-functions/functions-m-r/queryfilter.html
Code based on comment
get = request.getYN.filter(function () {
return (lang_code == this.lang && yn_code == code);
}).yn_description;
BTW: Unless the field are large text, varchar(max), or xml, It typically does matter if you are picking one or all
I am running ColdFusion 9 and Oracle database. I am trying to group all files from a directory by the dateLastModified using:
<cfdirectory action="LIST"
directory="#path_to_files#"
name="res"
sort="datelastmodified desc,name ASC"
filter="#filters#">
For example:
Date of File 1: 2016-10-03 11:49:00 am
Date of File 2: 2016-10-03 07:49:00 am
Date of File 3: 2016-08-03 07:49:00 am
File 1 & 2 should be group 1 and File 3 should be group 2
If I use a query of query, how can I compare the date based on days not time? This is what I have so far but it does not seem to work. I get no records.
<cfquery name="getfiles" dbtype="query">
SELECT name, datelastmodified
FROM res
WHERE CAST( [datelastmodified] as date) = CAST(<cfqueryparam cfsqltype="CF_SQL_DATE" value="#d#"/> as date)
</cfquery>
Does anyone know a different method to group files based on days not time?
method to group files based on days not time
To group by Date (only), add the "date only" column to your SELECT list. Then ORDER BY the new column:
<cfquery name="qSortedFiles" dbtype="query">
SELECT CAST(DateLastModified AS Date) AS DateOnly
, Name
, DateLastModified
FROM getFiles
ORDER BY DateOnly
</cfquery>
<cfoutput query="qSortedFiles" group="DateOnly">
#DateOnly#<br>
<cfoutput>
- #DateLastModified#<br>
</cfoutput>
</cfoutput>
I get no records.
FWIW, the reason is that despite using CAST ... as Date, apparently CF still preserves the "time" portion internally. So the WHERE clause is still comparing apples and oranges:
<!--- comparing Date and Time to Date (only)
WHERE 2016-08-03 07:49:00 am = 2016-08-03 00:00:00 am
Instead of using an equals comparison, use this approach:
WHERE DateLastModified >= {TheStartDateAtMidnight}
AND DateLastModified < {TheNextDayAtMidnight}
This type of comparison is more flexible as it works with both date and time columns - and dates (only).
WHERE CAST(DateLastModified AS Date) >= <cfqueryparam value="#someDate#" cfsqltype="cf_sql_date">
AND CAST(DateLastModified AS Date) < <cfqueryparam value="#dateAdd('d', 1, someDate)#" cfsqltype="cf_sql_date">
Step 1 - Use cfdirectory to get a query object.
Step 2 - Add a column using queryaddcolumn.
Step 3 - Loop through the query. Use querySetCell and dateformat on your new column
Step 4 - use cfoutput, group by your new column, to do what you need to do. Your cfdirectory tag already has the data sorted.
So I figured it out. Thanks to #Dan and #Leigh for their suggestion. I used both as guides to get what I wanted.
I used cfdirectory to get a query object.
I created a new query using QueryNew, QueryAddRow and QuerySetCell.
On the columns of the new query contained the formatted date (mm/dd/yyyy). Make sure that you declare the column as varchar not date when setting the column names QueryNew.
I used cfloop and group option and cfoutput to display the records.
<cfset path_to_files = "d:\inetpub\wwwroot\mailrideli\webrpt\">
<cfset filters = "*.pdf|*.txt|*.xls">
<cfdirectory action="LIST" directory="#path_to_files#" name="res" sort="datelastmodified desc,name ASC" filter="#filters#">
<cfset q = QueryNew("Name,dateformated,datelastmodified, size","Varchar, Varchar, date, varchar")>
<cfset count = 1>
<cfset newRow = QueryAddRow(q,res.recordCount)>
<cfloop query="res">
<cfset d = DateFormat(res.dateLastModified,"mm/dd/yyyy")>
<cfset temp = QuerySetCell(q, "Name", "#res.name#", count)>
<cfset temp = QuerySetCell(q, "dateformated", "#d#", count)>
<cfset temp = QuerySetCell(q, "datelastmodified", "#res.datelastmodified#", count)>
<cfset temp = QuerySetCell(q, "size", "#res.size#", count)>
<cfset count += 1>
</cfloop>
<cfoutput>
<cfloop query="q" group="dateformated">
#q.dateformated#<br />
<table>
<tr>
<th>File Name</th>
<th>Size (bytes)</th>
<th>Last Modified</th>
</tr>
<cfloop>
<tr>
<td>#q.name#</td>
<td>#q.size#</td>
<td>#dateformat(q.dateLastModified, 'mm/dd/yyyy')# #timeformat(q.dateLastModified, 'hh:mm:ssTT')#</td>
</tr>
</cfloop>
</table>
</cfloop>
</cfoutput>
I hope it helps anyone out there.
I have a form containing 6 check_num fields, 1 account number and an amount. I would like to insert those values into a database table:
<cfquery datasource="test" name="test">
insert into test (ACCOUNT_NUMBER,check_num,amount)
values ('#ACCOUNT_NUMBER#','#check_num#','#amount#')
</cfquery>
However, I don't want to insert all of the values into 1 row. If any of the check_num fields contain a number, I want to insert that value, along with with the acccount_number and amount values.
For example with the data below, I would like to insert 5 rows. The field "check_num6" would be skipped because it is empty.
ACCOUNT_NUMBER 123456789
CHECK_NUM1 56623
CHECK_NUM2 5512
CHECK_NUM3 6562
CHECK_NUM4 653
CHECK_NUM5 6623
CHECK_NUM6
AMOUNT 32.31
FIELDNAMES ACCOUNT_NUMBER,CHECK_NUM1,CHECK_NUM2,CHECK_NUM3,CHECK_NUM4,CHECK_NUM5,CHECK_NUM6,AMOUNT,SUBMIT
SUBMIT Submit
Not perfect but you can start/get some inspiration from here:
<cfloop list="#StructKeyList(form)#" index="i">
<cfif (left('#i#', 9) is "CHECK_NUM") and (trim(form['#i#']) is not "")>
<cfquery datasource="test" name="test">
insert into test (ACCOUNT_NUMBER,check_num,amount)
values ('#ACCOUNT_NUMBER#','#form['#i#']#','#amount#')
</cfquery>
</cfif>
</cfloop>
Also you should validate, see if your DB supports multiple inserts, change your query to use cfqueryparam, research about it. So, a better cfquery code would be:
<cfquery datasource="test" name="test">
insert into test (
ACCOUNT_NUMBER,
check_num,
amount
)
values (
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.ACCOUNT_NUMBER#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form['#i#']#">,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.amount#">
)
</cfquery>
I wrote a script that takes date entries and for some reason whenever I specify just a starting date with a blank end date the query never picks it up. Here's what I wrote.
<cfquery name="stec_mysql_loan_tracking_qry" result="meta_tracking" datasource="STLinux1MySQL">
Select
tslo.created,
tslo.created_by,
tslo.last_modified,
tslo.last_modified_by,
tslo.active,
tslo.is_manager,
tslo.pick_userid,
tslo.customer_code,
tslo.name,
tst.user_ip as ip,
tsl.loan_identifier,
tst.command,
tsl.tax_search_loan_id as id
From
tax_search_loan_officers tslo Left Join
tax_search_loans tsl On tsl.tax_search_loan_officer_id =
tslo.tax_search_loan_officer_id Left Join
tax_search_track tst On tst.pick_userid = tslo.pick_userid
Where
tslo.customer_code In (<cfqueryparam value="#tw_custcodes#" cfsqltype="cf_sql_varchar" list="yes">)
<cfif IsDefined('url.active')>
<cfif url.active neq "">
AND
tslo.active = <cfqueryparam value="#Trim(url.active)#" cfsqltype="cf_sql_varchar" list="yes">
</cfif>
</cfif>
<cfif IsDefined('url.is_managed')>
<cfif url.is_managed neq "">
AND
tslo.is_manager = <cfqueryparam value="#Trim(url.is_managed)#" cfsqltype="cf_sql_varchar" list="yes">
</cfif>
</cfif>
<cfif IsDefined('url.start_end')>
<cfif url.start_date neq "" and url.end_date eq "">
AND
<cfqueryparam value="#Trim(url.start_date)#" cfsqltype="cf_sql_date"> <= DATE_FORMAT(tslo.last_modified, '%Y-%m-%d')
AND
DATE_FORMAT(tslo.last_modified, '%Y-%m-%d') <= DATE_FORMAT(NOW(), '%Y-%m-%d')
</cfif>
</cfif>
<cfif IsDefined('url.start_date')>
<cfif url.end_date neq "" and url.start_date eq "">
AND
'2012-01-01' <= DATE_FORMAT(tslo.last_modified, '%Y-%m-%d')
AND
DATE_FORMAT(tslo.last_modified, '%Y-%m-%d') <= <cfqueryparam value="#Trim(url.end_date)#" cfsqltype="cf_sql_date">
</cfif>
</cfif>
<cfif isDefined('url.start_date')>
<cfif (url.start_date neq "") and (url.end_date neq "")>
AND
<cfqueryparam value="#Trim(url.start_date)#" cfsqltype="cf_sql_date"> <= DATE_FORMAT(tslo.last_modified, '%Y-%m-%d')
AND
DATE_FORMAT(tslo.last_modified, '%Y-%m-%d') <= <cfqueryparam value="#Trim(url.end_date)#" cfsqltype="cf_sql_date">
</cfif>
</cfif>
</cfquery>
And here is what it sees if url.end_date = "" but url.start_date = a value:
Select
tslo.created,
tslo.created_by,
tslo.last_modified,
tslo.last_modified_by,
tslo.active,
tslo.is_manager,
tslo.pick_userid,
tslo.customer_code,
tslo.name,
tst.user_ip as ip,
tsl.loan_identifier,
tst.command,
tsl.tax_search_loan_id as id
From
tax_search_loan_officers tslo Left Join
tax_search_loans tsl On tsl.tax_search_loan_officer_id =
tslo.tax_search_loan_officer_id Left Join
tax_search_track tst On tst.pick_userid = tslo.pick_userid
Where
tslo.customer_code In (?)
However, every other combination is fine. I've tried rewriting the cfif blocks but this structure is the only one that gets 2/3 while the rest fail.
(From the comments ..)
<cfif IsDefined('url.start_end')>
It looks like you have three date variables: url.start_date, url.end_date and url.start_end. What is url.start_end - is it a typo?
As an aside, you might want to set defaults values for the variables so you could eliminate some of the cfif conditions. Then work on simplifying the rest of the logic, because it seems more complex than is necessary ... Dan's response contains some good suggestions. I strongly suspect you could simplify the code and make the query more efficient to boot by getting rid of the DATE_FORMAT(ColumnName, '%Y-%m-%d') statements, because they will prevent the database from properly utilizing indexes on the referenced column.
Update:
After taking a closer look, I think this is what the code is trying to accomplish:
If both dates are present, filter on the given values
If only one of the dates is present, apply a default for the missing date. Then filter on both values.
If neither date is present, skip the filtering.
Something along these lines should mimic the behavior of current code. Note, it uses this type of comparison as a more index-friendly way of handling "time" issues:
WHERE column >= {startDateAtMidnight}
AND column < {dayAfterEndDateAtMidnight}
Example:
<!--- default both to something that is NOT a valid date --->
<cfparam name="url.start_date" default="">
<cfparam name="url.end_date" default="">
<!---
If at least ONE of the dates was supplied, apply
the desired defaults for missing values
--->
<cfif isDate(url.start_date) || isDate(url.end_date)>
<cfset url.start_date = isDate(url.start_date) ? url.start_date : "some default like 2012-01-01 here">
<cfset url.end_date = isDate(url.end_date) ? url.end_date : now()>
</cfif>
<cfquery ....>
SELECT ...
FROM ...
WHERE ...
<!--- apply the filter when both dates are populated. --->
<cfif isDate(url.start_date) and isDate(url.end_date)>
AND tslo.last_modified >= <cfqueryparam value="#url.start_date#" cfsqltype="cf_sql_date">
AND tslo.last_modified < <cfqueryparam value="#dateAdd('d', 1, url.end_date)#" cfsqltype="cf_sql_date">
</cfif>
</cfquery>
It might not be the cause, but you are mixing data types. This:
and <cfqueryparam value="#Trim(url.start_date)#" cfsqltype="cf_sql_date">
<= DATE_FORMAT(tslo.last_modified, '%Y-%m-%d')
will have a date on the left hand side of your comparison operator and a string on the right. Even if it runs without error, you might get unexpected results. As a minimum, remove the date_format function from the right hand side.
Then we have this:
AND DATE_FORMAT(tslo.last_modified, '%Y-%m-%d') <= DATE_FORMAT(NOW(), '%Y-%m-%d')
at least it's comparing a string to a string, but it's inefficient. In the overall scheme of things, maybe you want something like this:
and tslo.last_modified >=
<cfqueryparam value="#Trim(url.start_date)#" cfsqltype="cf_sql_date">
and tslo.last_modified =< now()