I have a timestamp field in a MySQL table. I am trying to query the table using the following SQL query:
SELECT login_mode,count(login_mode) as total
FROM login_activity,ccac_registered_users
WHERE login_activity.student_id=ccac_registered_users.student_id
AND login_date >= STR_TO_DATE('01/16/2013','%m/%d/%Y')
AND login_date <= STR_TO_DATE('01/17/2013','%m/%d/%Y')
GROUP BY login_mode
The query works fine when I run it on Mysql directly, but doesn’t work from the coldfusion app. I am stumped! The code to generate the dates is :
login_date >= STR_TO_DATE(
'#DateFormat(CreateODBCDate(startDate),'mm/dd/yyyy')#'
, '%m/%d/%Y'
)
And the error from ColdFusion is this:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near ’01/16/2013”,”%m/%d/%Y”) AND login_date <=
STR_TO_DATE(”01/17/2013”,”%m/%d/’ at line 1
What is wrong here?
Edit
I just dumped the sql that CF is trying to execute.
SELECT login_mode,count(login_mode) as total
FROM login_activity,ccac_registered_users
WHERE login_activity.student_id=ccac_registered_users.student_id
AND login_date >= STR_TO_DATE(''01/16/2013'',''%m/%d/%Y'')
AND login_date <= STR_TO_DATE(''01/17/2013'',''%m/%d/%Y'')
There are additional quotes around the date and they are causing the issue. If I remove the extra ones I have added from the code, then nothing gets added.
CFQUERY likes to automatically escape single quotes. To get it to work the way you’ve written it, you’ll need to wrap things in
preserveSingleQuotes()to prevent CF from adding the extra single quotes. Or rewrite it using one of the other suggestions for date comparisons.