Home » SQL & PL/SQL » SQL & PL/SQL » convert comma separated string to IN clause (Oracle version 11g R2)
convert comma separated string to IN clause [message #631315] Mon, 12 January 2015 11:09 Go to next message
robh0502
Messages: 5
Registered: January 2015
Location: Phoenix, AZ
Junior Member
Hi,
I'd like to use a string in a table column like the following and use it as an IN clause for another query (without using dynamic SQL).

String example (from table column): ABC,DEF,XYZ

Use result in another query

select 'test'
from   tableB
where  column1 in (put string example here)
Re: convert comma separated string to IN clause [message #631317 is a reply to message #631315] Mon, 12 January 2015 11:10 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

This is a FAQ, please search for "varying in list".

Re: convert comma separated string to IN clause [message #631318 is a reply to message #631315] Mon, 12 January 2015 11:12 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
You can try as mentioned here https://lalitkumarb.wordpress.com/2015/01/02/varying-in-list-of-values-in-where-clause/
Re: convert comma separated string to IN clause [message #631319 is a reply to message #631315] Mon, 12 January 2015 11:59 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

SQL> var mylist varchar2(100)
SQL> exec :mylist := '5,11,13,22,23,31,44,45'

PL/SQL procedure successfully completed.

SQL> select substr(:mylist,
  2                instr(','||:mylist||',', ',', 1, rn),
  3                instr(','||:mylist||',', ',', 1, rn+1)
  4                - instr(','||:mylist||',', ',', 1, rn) - 1) value
  5  from ( select level rn from dual 
  6         connect by level 
  7                      <= length(:mylist)-length(replace(:mylist,',',''))+1
  8       )
  9  /
VALUE
-----------------------------------------------------------
5
11
13
22
23
31
44
45



SQL> var mylist varchar2(100)
SQL> exec :mylist := '5,11,13,22,23,31,44,45'
SQL> with list as (
  2    select substr(:mylist,
  3                  instr(','||:mylist||',', ',', 1, rn),
  4                  instr(','||:mylist||',', ',', 1, rn+1)
  5                  - instr(','||:mylist||',', ',', 1, rn) - 1) value
  6    from (select level rn from dual 
  7          connect by level 
  8                       <= length(:mylist)-length(replace(:mylist,',',''))+1)
  9    )
 10  select id, valeur
 11  from t
 12  where id IN ( select value from list )
 13  order by id
 14  /
        ID USERNAME
---------- ------------------------------
         5 SYSTEM
        11 OUTLN
        22 MICHEL
        23 OPS$MCADOT101205
        31 SCOTT

5 rows selected.

Re: convert comma separated string to IN clause [message #631320 is a reply to message #631319] Mon, 12 January 2015 12:38 Go to previous messageGo to next message
robh0502
Messages: 5
Registered: January 2015
Location: Phoenix, AZ
Junior Member
This worked out well. Thanks.
Re: convert comma separated string to IN clause [message #631333 is a reply to message #631320] Mon, 12 January 2015 14:22 Go to previous message
Solomon Yakobson
Messages: 3312
Registered: January 2010
Location: Connecticut, USA
Senior Member
XMLTABLE solution:

var mylist varchar2(100)
exec :mylist := '5,11,13,22,23,31,44,45';
select  *
  from  xmltable(
                 'for $s at $i in ora:tokenize(.,",")
                  where $i > 1
                  return $s
                 '
                 passing ',' || :mylist
                )
/

COLUMN_VALUE
----------------
5
11
13
22
23
31
44
45

8 rows selected.

SQL> 


SY.
Previous Topic: LOB
Next Topic: Rollback in DBMS_PARALLEL_EXECUTE
Goto Forum:
  


Current Time: Tue Aug 25 23:25:20 CDT 2026