Query a comma delimited list

Suppliers is a field containing a comma delimited list of
Supplier ID's.
When a supplier logs in they should be able to view all the
auctions that they have been registered for
i.e if their supplierID is in the suppliers field.
have tried this and get an error:
<CFQUERY NAME="GetAuctions"
DATASOURCE="#Application.Datasource#">
SELECT * FROM Auctions
WHERE '#Session.SupplierID#' IN 'Auctions.Suppliers'
</CFQUERY>
have tried this and recordcount is 0 when it should be 3:
<CFQUERY NAME="GetAuctions"
DATASOURCE="#Application.Datasource#">
SELECT * FROM Auctions
WHERE '#Session.SupplierID#' LIKE 'Auctions.Suppliers'
</CFQUERY>

You should avoid having a list value in a field and normalise
your table. But if you want to stick with your style(which is not
advisable), maybe you can do this. I believe your supplier id is a
string so the code below may cause slowness in your system:
<CFQUERY NAME="GetAuctions1"
DATASOURCE="#Application.Datasource#">
SELECT Suppliers FROM Auctions
</CFQUERY>
<cfoutput query="GetAuctions1">
<CFQUERY NAME="GetAuctions2"
DATASOURCE="#Application.Datasource#">
SELECT * FROM Auctions
WHERE '#Session.SupplierID#' IN(<cfqueryparam
values="#Suppliers#" cfsqltype="CF_SQL_VARCHAR" list="Yes">)
</CFQUERY>
</cfoutput>
But if your supplier id is a numeric value. then you can do
this:
<CFQUERY NAME="GetAuctions"
DATASOURCE="#Application.Datasource#">
SELECT A1.* FROM Auctions A1
WHERE #Session.SupplierID# IN(SELECT A2.Suppliers FROM
Auctions A2 WHERE A2.your_primary_key_for_table_Auctions =
A1.your_primary_key_for_table_Auctions)
</CFQUERY>

Similar Messages

  • REGEXP_SUBSTR for comma delimited list with null values

    Hi,
    I have a column which stores a comma delimited list of values. Some of these values in the list may be null. I'm having some issues trying to extract the values using the REGEXP_SUBSTR function when null values are present. Here are two things that I've tried:
    SELECT
       REGEXP_SUBSTR (val, '[^,]*', 1, 1) pos1
      ,REGEXP_SUBSTR (val, '[^,]*', 1, 2) pos2
      ,REGEXP_SUBSTR (val, '[^,]*', 1, 3) pos3
      ,REGEXP_SUBSTR (val, '[^,]*', 1, 4) pos4
      ,REGEXP_SUBSTR (val, '[^,]*', 1, 5) pos5
    FROM (SELECT 'AAA,BBB,,DDD,,FFF' val FROM dual);
    POS P POS P P
    AAA   BBB
    SELECT
       REGEXP_SUBSTR (val, '[^,]+', 1, 1) pos1
      ,REGEXP_SUBSTR (val, '[^,]+', 1, 2) pos2
      ,REGEXP_SUBSTR (val, '[^,]+', 1, 3) pos3
      ,REGEXP_SUBSTR (val, '[^,]+', 1, 4) pos4
      ,REGEXP_SUBSTR (val, '[^,]+', 1, 5) pos5
    FROM (SELECT 'AAA,BBB,,DDD,,FFF' val FROM dual);
    POS POS POS POS P
    AAA BBB DDD FFFAs you can see neither of the calls works correctly. Does anyone know how to modify the regular expression pattern to handle null values? I've tried various other patterns but was unable to get anyone to work for all cases.
    Thanks,
    Martin
    http://www.ClariFit.com
    http://www.TalkApex.com

    Hi, Martin,
    This does what you want:
    SELECT
       RTRIM (REGEXP_SUBSTR (val, '[^,]*,', 1, 1), ',') pos1
      ,RTRIM (REGEXP_SUBSTR (val, '[^,]*,', 1, 2), ',') pos2
      ,RTRIM (REGEXP_SUBSTR (val, '[^,]*,', 1, 3), ',') pos3
      ,RTRIM (REGEXP_SUBSTR (val, '[^,]*,', 1, 4), ',') pos4
      ,RTRIM (REGEXP_SUBSTR (val || ','
                          , '[^,]*,', 1, 5), ',') pos5
    FROM (SELECT 'AAA,BBB,,DDD,,FFF' val FROM dual);The query above works in Oracle 10 or 11, but in Oracle 11, you can also do it with just REGEXP_SUBSTR, without using RTRIM:
    SELECT
       REGEXP_SUBSTR (val, '([^,]*),|$', 1, 1, NULL, 1) pos1
      ,REGEXP_SUBSTR (val, '([^,]*),|$', 1, 2, NULL, 1) pos2
      ,REGEXP_SUBSTR (val, '([^,]*),|$', 1, 3, NULL, 1) pos3
      ,REGEXP_SUBSTR (val, '([^,]*),|$', 1, 4, NULL, 1) pos4
      ,REGEXP_SUBSTR (val, '([^,]*),|$', 1, 5, NULL, 1) pos5
    FROM (SELECT 'AAA,BBB,,DDD,,FFF' val FROM dual);The problem with your first query was that it was looking for sub-strings of 0 or more non-commas. There was such as sub-string. consisting of 3 characters, starting at position 1, so it returned 'AAA', as expected. Then there was another sub-string, of 0 characters, starting at position 4, so it returned NULL. Then there was a sub-string of 3 characters starting at position 5, so it returned 'BBB'.
    The problem with your 2nd query was that it was looking for 1 or more non-commas. 'DDD' is the 3rd such sub-string.
    Edited by: Frank Kulash on Feb 16, 2012 11:36 AM
    Added Oracle 11 example

  • Too many commas in my comma delimited list

    I'm trying to merge several pdf files into one by using the cfpdf tag (action="merge").  In the source attribute, you can enter a comma delimited list of file paths to merge the pdf files together.  I'm thinking that Adobe could have picked a better delimiter though because it breaks if there is a comma any one of the file names.  I've tried using replace() to replace the commas in my filenames with chr(44) before passing it to the cfpdf tag, but it still breaks.  Any ideas on how to accommodate this?  I'm trying to prevent having to copy hundreds of files to a temp directory, then use the directory attribute instead, then delete the temp directory.  That just seems like such a waste of resources...
    Thanks!

    cfpdfparam is a WIN!  Thanks for the heads up, i had no idea that you could use the tag like that.
    <cfpdf 
    action = "merge"
    destination = "C:\Inetpub\mydir\secure\test\output_merge.pdf"
    overwrite = "yes">
    <cfpdfparam source = "C:\Inetpub\mydir\secure\test\0003. A-001 - Restaurant, Floor Plan.pdf">
    <cfpdfparam source = "#expandPath('/secure/test/0001. G-001 - General.pdf')#">
    <cfpdfparam source = "#expandPath('/secure/test/0002. G-101 - General Information.pdf')#"></cfpdf>
    Works Great, Thanks!

  • Is it possible to convert a table of values into a comma-delimited-list?

    Hi,
    I'd like to turn the following dataset:
    Parent | Child
    Charles | William
    Charles | Harry
    Anne | Peter
    Anne | Zara
    Andrew | Beatrice
    Andrew | Eugenie
    into this:
    Parent | Children
    Charles | Diana,Camilla
    Anne | Peter,Zara
    Andrew | Beatrice,Eugenie
    In other words, I'd like to take a list of values pertaining to some key and produce them as a comma-delimited-list.
    I know its is possible in T-SQL although the method is a bit of a nasty hack. Is it possible in PL-SQL?
    Thaks in advance
    Jamie

    Hi,
    With model clause (10g)
    with t  as(
    select 'Charles' parent, 'William' child from dual union
    select 'Charles', 'Harry' from dual union
    select 'Anne', 'Peter' from dual union
    select 'Anne', 'Zara' from dual union
    select 'Andrew', 'Beatrice' from dual union
    select 'Andrew', 'Eugenie' from dual
    select parent,substr(res,2) res
    from t
    model
    return updated rows
    partition by ( parent)
    dimension by ( row_number()over(partition by parent order by child) rn)
    measures(child, cast( null as varchar2(4000)) as res)
    rules
    iterate(100000)
    until(presentv(res[iteration_number+2],1,0)=0)
    ( res[0]=res[0]||','||child[iteration_number+1]);
    PARENT  RES                                              
    Anne    Peter,Zara                                       
    Andrew  Beatrice,Eugenie                                 
    Charles Harry,William                                    
    3 rows selected.

  • Use PL/SQL Table values as a comma delimited list in a in (...) clause

    Hi,
    One my procedure's parameters is a PL/SQL table of id's (NUMBER) that the GUI sends it, based on the user's selections.
    Now, I want to use the table's values in my select's where clause to return only the correct records.
    However,
    select ......
    from ....
    where id in (i_array_ids)
    doesn't work of course, and my attempts of transfering the ids to a comma delimited list of numbers (as opposed to a lenghty varchar2 string) that could work between the ( ) have all failed.
    Thanks

    And here's an example I gave with some more up-to-date syntax than in that old AskTom thread:
    Re: DYNAMIC WHERE CLAUSE in PROCEDURE

  • Validating comma-delimited list for numeric entries

    Hi!
    I need to validate a comma-delimited list to make sure all the list items are numeric.  I could do a cfloop to loop through the list, then an IsValid() to check each entry, but that seems cumbersome, so I was wondering if there was a better way.
    Thanks!

    BreakawayPaul wrote:
    I had the idea to just do a replace() to get rid of all the commas and treat the value as one big number.  It seems to work.
    True. That is a creative test. But what if the list begins with 0 or contains negative or decimal numbers?
    If you must include those such eventualities, then you could extend the test to something like
    <cftry>
    <cfset myList="0,1,2,x">
    <cfset maxNo=arrayMax(listToArray(myList))>
    <!--- The rest of the business code goes here --->
    <cfcatch type="expression">
    <cfoutput>#cfcatch.Detail#</cfoutput>
    </cfcatch>
    </cftry>

  • Adding spaces in comma-delimited lists

    I'm pulling from a database comma-delimited db that appears
    like so:
    Carpet,Hardwood,Tile
    How can I add spaces after the comma (if there's commas at
    all)?

    #Replace(yourstringorvar, ",", ", ", "ALL")#
    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com

  • Comma delimited lists

    The data is as below:
    create table mylist
    id number,
    list varchar2(50)
    begin
    insert into mylist (id,list) values (111,'1,2');
    insert into mylist (id,list) values (222,'2,3');
    insert into mylist (id) values (333);
    commit;
    end;
    create table mylistvalues
    value number
    begin
    insert into mylistvalues (value) values (1);
    insert into mylistvalues (value) values (2);
    insert into mylistvalues (value) values (3);
    insert into mylistvalues (value) values (4);
    commit;
    end;
    I have to populate all ids in mylist table where list has one valid value in mylistvalues table.
    The output should be 111 and 222, because 1,2,3 are in mylistvalues table.
    Thanks.

    SELECT  DISTINCT id
      FROM  mylist,
            mylistvalues
      where regexp_like(',' || list || ',',',' || value || ',')
            ID
           222
           111
    SQL> SY.

  • Selecting from a comm delimited list

    Often we run queries like
    SELECT * FROM TABLENAME WHERE FIELDA NOT IN ('A','B','C');
    But what if your question goes in the opposite direction, what syntax would work?
    In other words. suppose I want to know which values in the list are not in the table. How could I find that out without having to create a temp table?
    I'm thinking that something like the following might work:
    SELECT FIELDA FROM ('A','B','C') A
    WHERE NOT EXISTS (SELECT 1 FROM TABLENAME B
    WHERE B.FIELDA = A.FIELDA)

    If you have a list of 30,000 items that you want to consider, then you'll likely want to have those items in a table. Here I'll create a fake table of orders, then create a shipment for most of those orders. Then I'll ask which orders of order type "PACKAGE BODY" have not been shipped.
    SQL> create table orders as
      2  select object_id AS order_id,
      3         object_type as order_type,
      4         owner as customer
      5    from all_objects;
    Table created.
    SQL>
    SQL> create table shipments as
      2  select *
      3    from orders
      4   where mod(order_id,47) > 0;
    Table created.
    SQL>
    SQL> select order_id
      2    from orders
      3   where order_type = 'PACKAGE BODY'
      4  minus
      5  select order_id
      6    from shipments
      7   where order_type = 'PACKAGE BODY';
      ORDER_ID
          3290
          3619
          3948
          4888
          5499
          5546
         25098
         25192
         29422
         32618
    10 rows selected.
    SQL>

  • Make a string into a comma delimited list

    I imagine someone out there has done this before, I am
    struggling with the right code to get it done. Please see code, any
    suggestions welcome and much appreciate in advance - thanks

    Try the code below. At the end of the loop, remove the extra
    comma at the end of the string.

  • Convert query results to a list

    Have a query which returns 20 records or so. I need to
    convert the results to a list and then search through the list to
    populate checkboxes. Simple query which returns 20 records:
    <CFQUERY NAME="GetTest" datasource="test">
    SELECT * FROM data
    WHERE brid = '012345'
    </CFQUERY>
    How can I convert these results to a list so I can use
    listfind?

    <CFSET foo = ValueList(getTest.bar)> converts the
    database field bar to the comma-delimited list foo.

  • Delimited list output

    I'd like to get a comma-delimited list of user email addresses from a table. I want to do it in a single select since I'm going to embed it into anther select. In SQL Server the following works:
    create table users (user_id varchar(10), email varchar(100))
    declare @email varchar(2000)
    SELECT @email = @email + email + '; ' FROM users
    How can I do this in Oracle (8i)?

    It sounds like you need Tom Kyte's str2tbl function as well as his stragg function. In the following example, I just left out the additional tables and join conditions, but it should work the same with them.
    scott@ORA92> -- test table and test data:
    scott@ORA92> create table hts_assay -- ha
      2    (assay_name varchar2(5),
      3       assay_id   varchar2(10),
      4       assay_type     varchar2(100))
      5  /
    Table created.
    scott@ORA92> insert all
      2  into hts_assay values ('name1', 'id1', 'type1a')
      3  into hts_assay values ('name1', 'id1', 'type1b')
      4  into hts_assay values ('name1', 'id1', 'type1c')
      5  into hts_assay values ('name2', 'id2', 'type2a')
      6  into hts_assay values ('name2', 'id2', 'type2b')
      7  into hts_assay values ('name3', 'id3', 'type3')
      8  into hts_assay values ('name4', 'id4', 'type4')
      9  select * from dual
    10  /
    7 rows created.
    scott@ORA92> commit
      2  /
    Commit complete.
    scott@ORA92> --
    scott@ORA92> -- code from Tom Kyte for stragg function:
    scott@ORA92> create or replace type string_agg_type as object
      2  (
      3       total varchar2(4000),
      4 
      5       static function
      6            ODCIAggregateInitialize(sctx IN OUT string_agg_type )
      7            return number,
      8 
      9       member function
    10            ODCIAggregateIterate(self IN OUT string_agg_type ,
    11                        value IN varchar2 )
    12            return number,
    13 
    14       member function
    15            ODCIAggregateTerminate(self IN string_agg_type,
    16                          returnValue OUT  varchar2,
    17                          flags IN number)
    18            return number,
    19 
    20       member function
    21            ODCIAggregateMerge(self IN OUT string_agg_type,
    22                      ctx2 IN string_agg_type)
    23            return number
    24  );
    25  /
    Type created.
    scott@ORA92> create or replace type body string_agg_type
      2  is
      3 
      4  static function ODCIAggregateInitialize(sctx IN OUT string_agg_type)
      5  return number
      6  is
      7  begin
      8        sctx := string_agg_type( null );
      9        return ODCIConst.Success;
    10  end;
    11 
    12  member function ODCIAggregateIterate(self IN OUT string_agg_type,
    13                             value IN varchar2 )
    14  return number
    15  is
    16  begin
    17        self.total := self.total
    18        || ','
    19        || value;
    20        return ODCIConst.Success;
    21  end;
    22 
    23  member function ODCIAggregateTerminate(self IN string_agg_type,
    24                               returnValue OUT varchar2,
    25                               flags IN number)
    26  return number
    27  is
    28  begin
    29        returnValue := ltrim(self.total,',');
    30        return ODCIConst.Success;
    31  end;
    32 
    33  member function ODCIAggregateMerge(self IN OUT string_agg_type,
    34                           ctx2 IN string_agg_type)
    35  return number
    36  is
    37  begin
    38        self.total := self.total || ctx2.total;
    39        return ODCIConst.Success;
    40  end;
    41 
    42 
    43  end;
    44  /
    Type body created.
    scott@ORA92> CREATE or replace
      2  FUNCTION stragg(input varchar2 )
      3  RETURN varchar2
      4  PARALLEL_ENABLE AGGREGATE USING string_agg_type;
      5  /
    Function created.
    scott@ORA92> -- code from Tom Kyte for str2tbl function:
    scott@ORA92> create or replace type myTableType as table of varchar2(100);
      2  /
    Type created.
    scott@ORA92> create or replace function str2tbl( p_str in varchar2 )
      2  return myTableType
      3  as
      4        l_str      long default p_str || ',';
      5        l_n         number;
      6        l_data    myTableType := myTabletype();
      7  begin
      8        loop
      9            l_n := instr( l_str, ',' );
    10            exit when (nvl(l_n,0) = 0);
    11            l_data.extend;
    12            l_data( l_data.count ) := ltrim(rtrim(substr(l_str,1,l_n-1)));
    13            l_str := substr( l_str, l_n+1 );
    14        end loop;
    15        return l_data;
    16  end;
    17  /
    Function created.
    scott@ORA92> --
    scott@ORA92> -- usage of stragg and str2tbl functions within a procedure
    scott@ORA92> -- that has a ref cursor as an out parameter
    scott@ORA92> CREATE OR REPLACE PROCEDURE your_procedure
      2    (xcur_spcur  OUT sys_refcursor,
      3       p_assaylist IN     VARCHAR2)
      4  AS
      5  BEGIN
      6    OPEN xcur_spcur FOR
      7    SELECT ha.assay_name, STRAGG (ha.assay_type) AS AssayResultTypes
      8    FROM   hts_assay ha
      9    WHERE  ha.assay_id IN
    10             (SELECT *
    11              FROM   TABLE (CAST (str2tbl (p_assaylist) AS mytableType)))
    12    GROUP  BY ha.assay_name;
    13  END your_procedure;
    14  /
    Procedure created.
    scott@ORA92> SHOW ERRORS
    No errors.
    scott@ORA92> VARIABLE g_ref REFCURSOR
    scott@ORA92> VARIABLE g_assaylist VARCHAR2(60)
    scott@ORA92> EXEC :g_assaylist := 'id1,id2,id3'
    PL/SQL procedure successfully completed.
    scott@ORA92> EXECUTE your_procedure (:g_ref, :g_assaylist)
    PL/SQL procedure successfully completed.
    scott@ORA92> PRINT g_ref
    ASSAY ASSAYRESULTTYPES
    name1 type1a,type1b,type1c
    name2 type2a,type2b
    name3 type3

  • Form field with comma delimited value list to cfc

    I have a form that passes a field to an action page with a
    comma delimited value.
    For instance the field name is: Program_ID
    value for program_ID is: 31, 32
    I am able to treat this variable as a list and check its
    length, and loop over the list prior to passing it to a cfc using
    the attached code:
    When I try and pass the variable as a string to a cfc and
    invoke a query, cf no longer recognizes my var as a list.
    Therefore the code attached does not function...
    Is there a specific var type that will pass through as a list
    and allow me to run the code block attached?
    thanks
    Craig

    Ok answered my own question.. Here is the answer for those
    who are interested...
    initialize var for cfc in cfinvoke statement
    <cfinvokeargument name="Program_ID"
    value="#Form.Program_ID#">
    pass argument to cfc as a string
    <cfargument name="Program_ID" type="string"
    required="true">
    use listqualify to parse list in cfc and set new var
    <cfset selectedProgramID =
    ListQualify(Program_ID,"'",",","CHAR")>
    use the new var in the following statement in sql code:
    ((Program_ID) IN (#PreserveSingleQuotes(selectedProgramID)#))
    The following code handles a form field with a single value
    or a comma delimited value.

  • Comma delimited in Sql query decode function errors out

    Hi All,
    DB: 11.2.0.3.0
    I am using the below query to generate the comma delimited output in a spool file but it errors out with the message below:
    SQL> set lines 100 pages 50
    SQL> col "USER_CONCURRENT_QUEUE_NAME" format a40;
    SQL> set head off
    SQL> spool /home/xyz/cmrequests.csv
    SQL> SELECT
    2 a.USER_CONCURRENT_QUEUE_NAME || ','
    3 || a.MAX_PROCESSES || ','
    4 || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    5 ||sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'I',1,0),0)) Pending_Normal ||','
    6 ||sum(decode(b.PHASE_CODE,'R',decode(b.STATUS_CODE,'R',1,0),0)) Running_Normal
    7 from FND_CONCURRENT_QUEUES_VL a, FND_CONCURRENT_WORKER_REQUESTS b
    where a.concurrent_queue_id = b.concurrent_queue_id AND b.Requested_Start_Date <= SYSDATE
    8 9 GROUP BY a.USER_CONCURRENT_QUEUE_NAME,a.MAX_PROCESSES;
    || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    ERROR at line 4:
    ORA-00923: FROM keyword not found where expected
    SQL> spool off;
    SQL>
    Expected output in the spool /home/xyz/cmrequests.csv
    Standard Manager,10,0,1,0
    Thanks for your time!
    Regards,

    Get to work immediately on marking your previous questions ANSWERED if they have been!
    >
    I am using the below query to generate the comma delimited output in a spool file but it errors out with the message below:
    SQL> set lines 100 pages 50
    SQL> col "USER_CONCURRENT_QUEUE_NAME" format a40;
    SQL> set head off
    SQL> spool /home/xyz/cmrequests.csv
    SQL> SELECT
    2 a.USER_CONCURRENT_QUEUE_NAME || ','
    3 || a.MAX_PROCESSES || ','
    4 || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    5 ||sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'I',1,0),0)) Pending_Normal ||','
    6 ||sum(decode(b.PHASE_CODE,'R',decode(b.STATUS_CODE,'R',1,0),0)) Running_Normal
    7 from FND_CONCURRENT_QUEUES_VL a, FND_CONCURRENT_WORKER_REQUESTS b
    where a.concurrent_queue_id = b.concurrent_queue_id AND b.Requested_Start_Date <= SYSDATE
    8 9 GROUP BY a.USER_CONCURRENT_QUEUE_NAME,a.MAX_PROCESSES;
    || sum(decode(b.PHASE_CODE,'P',decode(b.STATUS_CODE,'Q',1,0),0)) Pending_Standby ||','
    >
    Well if you want to spool query results to a file the first thing you need to do is write a query that actually works.
    Why do you think a query like this is valid?
    SELECT 'this, is, my, giant, string, of, columns, with, commas, in, between, each, word'
    GROUP BY this, is, my, giant, stringYou only have one column in the result set but you are trying to group by three columns and none of them are even in the result set.
    What's up with that?
    You can only group by columns that are actually IN the result set.

  • DRM Function to divide comma delimited string to list value

    Hi
    Any one knows how to seperate comma delimited string into list values.
    Thanks

    Are you wanting to dynamically set the list values for a property? If so there are a couple options:
    1. You can update them via the API.
    2. You can write a sql script to update the table Property_List. This requires an application server restart and generally isn't recommended for support reasons but it makes sense in some instances.
    Edited by: Naren Truelove on 16/02/2011 22:08

Maybe you are looking for