Skip to main content
February 20, 2007
Answered

String value changes single quote ' to double quote "

  • February 20, 2007
  • 3 replies
  • 589 views
I am creating a list with different bill codes within single quotes as follows
<cfset corlist = " '1100 ','1200 ','1300 ','1700 ','1800 ','1950 ','7001 ' ">
when I do an output
for
<cfoutput>AND idbillcode IN ( #corlist #)</cfoutput>

I get the values as follows
AND idbillcode IN ( '1100 ','1200 ','1300 ','1700 ','1800 ','1950 ','7001 ')


However when I put the same string within a cfquery the single quotes get replaced by double quotes as follows

AND idbillcode IN ( ''1100 '',''1200 '',''1300 '',''1700 '',''1800 '',''1950 '',''7001 '') which throws an error.

Anybody has any clues.

Thanks.
This topic has been closed for replies.
Correct answer
I found the answer PreserveSingleQuotes. Thanks

3 replies

Inspiring
February 20, 2007
A better solution would be to use <cfqueryparam list="Yes">, that way to don't expose yourself to SQL injection (plus it helps your queries run faster):

AND idbillcode IN ( <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#corlist#" list="Yes">)

<cfqueryparam> automatically determines whether to add single quotes or not based on the cfsqltype - so your corlist can just be a comma-delimited list of numbers and CF will handle qualifying them.
Correct answer
February 20, 2007
I found the answer PreserveSingleQuotes. Thanks
Inspiring
February 20, 2007
However when I put the same string within a cfquery the single quotes
get replaced by double quotes as follows

AND idbillcode IN ( ''1100 '',''1200 '',''1300 '',''1700 '',''1800
'',''1950
'',''7001 '') which throws an error.

Anybody has any clues.

That is ColdFusion escaping the single quotes, by doubling them so that
you can search for strings such as "singhpk's code does not work".
(Note the single quote/apostrophe that would normally break this string
if it was not escaped.

To tell CF not to do this, one uses the preserveSingleQuotes() function.
The documentation has all the details.