Skip to main content
Participant
December 7, 2019
Question

Search queries with single quotes

  • December 7, 2019
  • 1 reply
  • 688 views

I'm doing a QoQ for last names and sometimes I run into O'Briens or O'Tooles and these won't search.

 

I'm using:

<cfset Variables.SearchName="%"&LCase(Trim(Form.Name))&"%">

(LOWER(LastName) LIKE <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Variables.SearchName#">

 

Any idea what I'm doing wrong?

This topic has been closed for replies.

1 reply

Charlie Arehart
Community Expert
Community Expert
December 7, 2019

Use the preservesinglequotes function, within your cfset, alongside the trim function. Let us know if it helps. 

/Charlie (troubleshooter, carehart. org)
Participant
December 8, 2019

I think I've found my problem. 

I was using:

WHERE ((LOWER(LastName) LIKE <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Variables.SearchName#">)

When Variables.SearchName="%smith%" or "%smi%" this works fine.

 

But if Variables.SearchName = "%o'brien%" or "%o'b%" it fails.  (Even with your idea of preservesinglequotes).

 

If I switch to:

WHERE ((LOWER(LastName) = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Variables.SearchName#">)

And Variables.SearchName = "o'brien" the it finds all O'Briens.

 

I'm using CF9 and and MS Access database. 

 

Any other ideas how I might get the wild cards working with names with apostrophes?

Charlie Arehart
Community Expert
Community Expert
December 8, 2019

Well, the reason that it works when you do the = (rather than like) is that then you're simply passing in a variable value to that =  test, and CF by default does do the equivalent of preservesinglequotes for you. The function is really for when you "build the SQL" outside the query and store it in a variable that you then refer to within the cfquery. Then it does NOT do the preservesinglequotes for you, and that's why you may need to add that in the code that is BUILDING that sql outside the cfquery.

 

And I thought that perhaps since you were adding the % in the cfset, that may lead CF to therefore require you to add the function.

 

But now when you say you have changed to just doing the =, are you really meaning that you have left the % in the CFSET? I would not expect ANY of the values to match in that case, since none of your table cols would have that % in the value (and = is indeed an equality match). Maybe you mean that you ALSO changed the CFSET to not use the % (even though you don't say so).

 

So why don't you try this:

 

WHERE ((LOWER(LastName) LIKE '%<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Variables.SearchName#">%')

 

Notice I have taken the like out of the cfset and put it in the SQL (where really it belongs). I am not sure it WILL work, but you have presuming you could put that in the variable that is passed to the cfqueryparam, and I would not be surprised if it did not work.

 

I proposed the preservesinglequotes as a first possible solution. Let us know if this second one may work for you.

/Charlie (troubleshooter, carehart. org)