• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

How do you check to see if query results contain a string?

I have two queries; the first one simply returns a list (of, say, all letters of the alphabet) and the second  returns a varying number of letters. What I want to do is print out the results of the first query highlighting the ones that are contained in the second query. I'm guessing it would be done something like the following:

<cfloop query="qAlphabet">
   <cfif [qSelectLetters contains qAlphabet.letter]><b></cfif>
   <cfif [qSelectLetters contains qAlphabet.letter]></b><cfif>

But I'm unsure of how to phrase the part in the brackets. I've done it by setting a string (looping through query 2, to give something like "a,c,g,w" and seeing if that string contains qAlphabet.letter, but it seems like that would be excessively huge if the queries were involving blocks of text, not just letters.
1 Solution
The syntax for your example would be something along the lines of:

<cfif LISTFIND(valuelist(qAlphabet.letter),qSelectLetters)>

But, you are correct in that it wouldn't be wise to use that solution for larger resultsets.

I would recommend doing the comparing at the SQL level. Either in the original query using a LEFT JOIN, or by using a query of queries.

Rough example of how you would use a LEFT JOIN to acheive the desired result:

   , CASE WHEN b.joinID IS NOT NULL THEN 1 ELSE 0 END as isrecordfound
FROM allrecordtable a
LEFT JOIN myvaluetable b
ON a.joinID = b.joinID

And your CFIF inside the output loop would be simply:

<cfif isrecordfound eq 1>


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now