Skip to main content
Participant
December 29, 2022
Question

How to get all records using IN operator in cfqueryparam in coldfusion?

  • December 29, 2022
  • 2 replies
  • 351 views

I have a query which I try to fetch records using IN operator. But the record retrieves only first data not all data . What I have tried is

<cfargument name="names" type="struct" >
SELECT
ID,
Name,
Title
FROM
tblabcd
WHERE
Active = 1
AND Name IN(<cfqueryparam value="#arguments.names.data#" cfsqltype="cf_sql_varchar" list="yes" separator="|">)
</cfquery>

Here arguments.names.data = abc data | efg name

Query return only data of "abc data"

How to return both data's from this query using IN operator.

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    December 29, 2022

    You have to trim each list element to remove any extra space. 

    <!--- Before the query tag, trim each list item of arguments.names.data --->
    <cfscript>    
           closure=function(listItem){ 
                       return trim(listItem); // Trims each list item
                   }
           // New list without excess spaces
           trimmedList=listMap(arguments.names.data,closure,"|");
    </cfscript>
    
    <!--- Within the query --->
    <cfqueryparam value="#trimmedList#" cfsqltype="cf_sql_varchar" list="yes" separator="|">
    Community Expert
    December 29, 2022

    Good catch!

     

    Dave Watts, Eidolon LLC 

    Dave Watts, Eidolon LLC
    Community Expert
    December 29, 2022

    What happens if you just put in "efg name" as a test value in place of the CFQUERYPARAM? Do you get any matching records?

     

    Dave Watts, Eidolon LLC 

    Dave Watts, Eidolon LLC
    abcd987Author
    Participant
    December 30, 2022

    Yes