Comma separated list for in ()
I am trying get a comma separated list of numbers (123,345,332,645) from a text field and pass it to a select statement that has in condition. The code looks something like
select a,b,c from tab1
where a in (:P1_SEARCH)
This works when I put only one value in the field. if I put two values like 345,453 it doesn't.
Can someone please explain this behaviour?
Thank you,
Kirtan Desai
Kirtan,
Lots of info here: Re: Search on a typed in list of values .
Scott
Similar Messages
-
How to get distinct values in a comma separated list of email addresses?
Hi Friends,
I have a cursor which fetches email address along with some other columns. More than one record can have same email address.
Ex
CURSOR C1 IS
SELECT 1 Buyer,'XX123' PO, '[email protected]' Buyer_email from dual
UNION ALL
SELECT 2 Buyer,'XX223' PO, '[email protected]' Buyer_email from dual
UNION ALL
SELECT 1 Buyer,'XX124' PO, '[email protected]' Buyer_email from dual
UNION ALL
SELECT 2 Buyer,'XX224' PO, '[email protected]' Buyer_email from dualNow, i open the cursor write the contents into a file and also form a comma separated list of buyer emails as follows
for cur_rec in c1
LOOP
--write contents into a file
l_buyer_email_list := l_buyer_email_list||cur_rec.buyer_email||',';
END LOOP
l_buyer_email_list := RTRIM(l_buyer_email_list,',');
The buyer email list will be like: '[email protected],[email protected],[email protected],[email protected]'
Inorder to avoid duplicate email address in the list, i can store each of this value is a table type variable and compare in each iteration whether the email already exist in the list or not.
Is there any other simpler way to achieve this?
Regards,
Sreekanth Munagala.If you are using oracle version 11, you can use listagg function
with c as
(SELECT 1 Buyer,'XX123' PO, '[email protected]' Buyer_email from dual
UNION ALL
SELECT 2 Buyer,'XX223' PO, '[email protected]' Buyer_email from dual
UNION ALL
SELECT 1 Buyer,'XX124' PO, '[email protected]' Buyer_email from dual
UNION ALL
SELECT 2 Buyer,'XX224' PO, '[email protected]' Buyer_email from dual
select buyer, listagg(buyer_email,',') within group (order by buyer)
from c
group by buyer
order by buyerFor prior versions
{cod}
with c as
(SELECT 1 Buyer,'XX123' PO, '[email protected]' Buyer_email from dual
UNION ALL
SELECT 2 Buyer,'XX223' PO, '[email protected]' Buyer_email from dual
UNION ALL
SELECT 1 Buyer,'XX124' PO, '[email protected]' Buyer_email from dual
UNION ALL
SELECT 2 Buyer,'XX224' PO, '[email protected]' Buyer_email from dual
select buyer, rtrim(xmlagg(xmlelement(e,buyer_email||',').extract('//text()')),',')
from c
group by buyer
order by buyer -
Obtaining comma-separated list of text values associated with bitwise flag column
In the table msdb.dbo.sysjobsteps, there is a [flags] column, which is a bit array with the following possible values:
0: Overwrite output file
2: Append to output file
4: Write Transact-SQL job step output to step history
8: Write log to table (overwrite existing history)
16: Write log to table (append to existing history)
32: Include step output in history
64: Create a Windows event to use as a signal for the Cmd jobstep to abort
I want to display a comma-separated list of the text values for a row. For example, if [flags] = 12, I want to display 'Write Transact-SQL job step output to step history, Write log to table (overwrite existing history)'.
What is the most efficient way to accomplish this?Here is a query that gives the pattern:
DECLARE @val int = 43
;WITH numbers AS (
SELECT power(2, n) AS exp2 FROM (VALUES(0), (1), (2), (3), (4), (5), (6)) AS n(n)
), list(list) AS (
SELECT
(SELECT CASE WHEN exp2 = 1 THEN 'First flag'
WHEN exp2 = 2 THEN 'Flag 2'
WHEN exp2 = 4 THEN 'Third flag'
WHEN exp2 = 8 THEN 'IV Flag'
WHEN exp2 = 16 THEN 'Flag #5'
WHEN exp2 = 32 THEN 'Another flag'
WHEN exp2 = 64 THEN 'My lucky flag'
END + ', '
FROM numbers
WHERE exp2 & @val = exp2
ORDER BY exp2
FOR XML PATH(''), TYPE).value('.', 'nvarchar(MAX)')
SELECT substring(list, 1, len(list) - 1)
FROM list
Here I'm creating the numbers on the fly, but it is better to have a table of numbers in your database. It can be used in many places, see here for a short discussion:
http://www.sommarskog.se/arrays-in-sql-2005.html#numbersasconcept
(Only read down to the next header.)
For FOR XML PATH thing is the somewhat obscure way we create concatenated lists. There is not really any using trying to explain how it works; it just works. The one thing to keep in mind is that it adds an extra comma at the end and the final query strips
it off.
This query does not handle that 0 has a special meaning - that is left as an exercise to the reader.
Erland Sommarskog, SQL Server MVP, [email protected] -
Problem using comma separated list with nested table element
Hi,
I have a comma separated list like that:
H23004,H24005,T7231,T8231,T9231And want to create a function which is creating a where clause for each element with an output like that:
UPPER('H23004') IN (UPPER(charge))
OR UPPER('H23005') IN (UPPER(charge))
OR UPPER('T7231') IN (UPPER(charge))
OR UPPER('T8231') IN (UPPER(charge))
OR UPPER('T9231') IN (UPPER(charge))Here is my test function which doesn't work correctly:
create or replace function FNC_LIST_TO_WHERE_CLAUSE(v_list in VARCHAR2) return varchar2 is
-- declaration of list type
TYPE batch_type IS TABLE OF pr_stamm.charge%TYPE;
-- variable for Batches
v_batch batch_type := batch_type('''' || replace(v_list,',',''',''') || '''');
return_script varchar2(1000);
BEGIN
-- loop as long as there are objects left
FOR i IN v_batch.FIRST .. v_batch.LAST
LOOP
--DBMS_OUTPUT.PUT_LINE(offices(i));
-- create where clause
IF i = 1 THEN
return_script := 'UPPER(' || v_batch(i) || ') IN (UPPER(charge))';
ELSE
return_script := return_script || ' OR UPPER(' || v_batch(i) || ') IN (UPPER(charge))';
END IF;
END LOOP;
return (return_script);
end;The out put looks like that:
UPPER('H23004','H24005','T7231','T8231','T9231') IN (UPPER(charge))I have no idea what I do wrong? It calculates the amount of array element wrong! (v_batch.LAST should be 5.)
v_batch.FIRST = 1
v_batch.LAST = 1
Regards,
Tobiastry this....
declare
text varchar2(1000) := 'H23004,H24005,T7231,T8231,T9231';
v_where varchar2(1000);
begin
text := text||',';
while instr(text,',') <> 0
loop
v_where := v_where || 'UPPER('''||substr(text,1,instr(text,',',1)-1)||''' IN (UPPER(charge)) OR ';
text := substr(text,instr(text,',',1)+1);
end loop;
v_where := substr(v_where,1,length(v_where)-3);
dbms_output.put_line(v_where);
end;
convert this one into function ... -
Unable to pass comma separated values for in clause
I have the following query : for :P_LEG_NUM Parameter when i am passing values like 1,2,5 as string type i am getting invalid number error... I have defined in clause for it but still it does not work.. For individual values like 2, etc it works... how can i pass comma separted values for this bind variable
select trip_number as prl_trip_number,
flight_number as prl_f_number,
trip_leg_id as prl_trip_leg_id,
leg_number as prl_leg_num,
dicao as prl_dicao,
etd_zulu as prl_etd_zulu,
aicao as prl_aicao,
eta_zulu as prl_eta_zulu,
to_char(etd_zulu,'DD-Mon-YYYY HH24:MI') as prl_cb_etd,
to_char(eta_zulu,'DD-Mon-YYYY HH24:MI') as prl_cb_eta,
diata as prl_diata,
aiata as prl_aiata,
(select client_name
from xxwfs_trip_header_details t_h
where t_h.trip_number = t_leg.trip_number) as prl_client_name,
(select to_char((select systimestamp at time zone 'GMT' from dual),'YYYY-MM-DD-HH24MI')
from dual) as prl_curr_zulu_date
from xxwfs_trip_leg_details t_leg
where 1=1
and t_leg.leg_number in nvl(:P_LEG_NUM,t_leg.leg_number)
and t_leg.trip_number = :P_trip_noThis is the problem know as <tt>'Varying IN List'</tt> issue.
Check this - SQL and PL/SQL FAQ -
Comma separated values for input and return multiple values
Hello everyone,
I have this simple package. Can someone suggest a way to accept multiple empno as input (comma separated) and to return set of salary values for the set of employee numbers (compatible to work with lower Oracle versions). Thanks much!
CREATE OR REPLACE PACKAGE test_multi IS
FUNCTION GET_sal(P_empno IN emp.empno%TYPE) RETURN NUMBER;
END test_multi;
CREATE OR REPLACE PACKAGE BODY test_multi IS
FUNCTION GET_sal(P_empno IN emp.empno%TYPE) RETURN NUMBER IS
V_sal NUMBER(10,2);
MSG VARCHAR2(200);
BEGIN
SELECT sal
INTO V_sal
FROM emp
WHERE empno = p_empno;
RETURN V_sal;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found.');
IF (V_sal IS NULL OR V_sal = 0) THEN
V_sal := 0;
END IF;
RETURN V_sal;
WHEN OTHERS THEN
MSG := SUBSTR(SQLERRM, 1, 70);
DBMS_OUTPUT.PUT_LINE(MSG);
END GET_sal;
END test_multi; -- End packageA way to do this in 10g or above...
SQL> ed
Wrote file afiedt.buf
1 with e as (select '7499,7698,7654,7902' as enos from dual)
2 --
3 select empno, sal
4 from emp
5 where empno in (select regexp_substr(enos,'[^,]+',1,rownum)
6 from e
7* connect by rownum <= length(regexp_replace(enos,'[^,]'))+1)
SQL> /
EMPNO SAL
7902 3000
7698 2850
7654 1250
7499 1600
SQL>As for Oracle 8, .... well.... like Oracle, I no longer use unsupported versions, so I'd recommend you upgrade to something that is supported. -
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> -
Localisation in comma separated list issue
Hello All,
I am having an issue with Localisation objects on an irpt page not getting populated. For example I am passing a list of Localisation objects to a query param like so (note the behaviour is the same weather I am passing to a param or just outputing the localization on the irpt):
"{##LOBJ_1},{##LOBJ_2},{##LOBJ_3},{##LOBJ_4},{##LOBJ_5},{##LOBJ_6}...."
I expect to get a result like the folowing:
"Text 1,Text 2,Text 3,Text 4,Text 5,Text 6..."
But I get a result like this where some values have been popluated and some haven't.
"Text 1,{##LOBJ_2},Text 3,Text 4,Text 5,{##LOBJ_2}..."
I have no issue displaying any of the objects on thier own on the irpt.
This issue is occuring on Version 12.0.4 Build(120), This issue does not occur for me on Version 12.0.2 Build(88)
Regards,
ChristianHi Christian,
We have the same problem with the SP04.
We made an OSS message on this problem.
SAP must deliver the SP05 version soon.
They have nevertheless said that the use of _ was discouraged.
Regards
Alexandre SEDANO -
Query to fetch Comma separated list
Hi all,
i have a table like users below.
I want to fetch the rows when to username is appeared in the in list.
SQL> select * from users;
PLATFORM USERS
1 raja,ram,sathish
2 raja,sathish
3 vena,meena,sathish
SQL> select * from users where lower(users)='raja,ram,sathish';
PLATFORM USERS
1 raja,ram,sathish
SQL> select * from users where lower(users) like '%raja%';
PLATFORM USERS
1 raja,ram,sathish
2 raja,sathishBut my requirement is even if the input list got changed,still it should retrieve that row..
here the order of raja,sathish and ram has got changed.. But i need to retrieve the record..
SQL> select * from users where lower(users) ='raja,sathish,ram';
no rows selectedIs the any function to do this?WITH users AS (SELECT 1 platform, 'raja,ram,sathish' users FROM DUAL
UNION ALL
SELECT 2, 'raja,sathish' FROM DUAL
UNION ALL
SELECT 3, 'vena,meena,sathish' FROM DUAL
UNION ALL
SELECT 4, 'raja,sathish,ram' FROM DUAL),
seach_string as (
select length(str) - length(replace(str,',')) +1 count_us,','||regexp_substr(str||',', '[[:alpha:]]+.,', 1, level) us
from
select 'raja,ram,sathish' str from dual
connect by level <= length(str) - length(replace(str,',')) +1
select *
from users
where users in
SELECT users
FROM users, seach_string
where instr(','||users||',', us) > 0
group by users
having count(*) = (select max(count_us) from seach_string)
PLATFORM USERS
1 raja,ram,sathish
4 raja,sathish,ram
2 rows selected. -
Combine data from a table and insert to another in comma separated format
In my SQL database table i have some data as shown below.
KNO Course Grade Institution
124 BTECH First IIT Calicut
128 BE First KKS Institute
124 CCNA Q NIIT Delhi
124 DDCN Q Appl Calicut
128 DIT A NIIT Delhi
128 VB Q IICM Delhi
i want this courses to be arranged as sample given under to a table named mainpers
KNO Course
124 BTECH[First], CCNA[Q],DDCN[Q]
128 BE[First], DIT[A], VB[Q]
I am a System Administrator at VadodaraYou can do this in SQL.
with cte as (
select distinct KNO
from Table1
select KNO
, Stuff(
(select ',' + Course + '[' + Grade + ']'
from Table1
where Table1.KNO = cte.KNO
for xml path ('')
), 1, 1, '') as Course
from cte
http://davidduffett.net/post/5334646215/get-a-comma-separated-list-of-values-in-sql-with-for
Russel Loski, MCT, MCSE Data Platform/Business Intelligence. Twitter: @sqlmovers; blog: www.sqlmovers.com -
Query on column with comma separated values
I have a proposed table with unnormalized data like the following:
ID COLA COLB REFLIST
21 xxx zzz 24,25,78,412
22 xxx xxx 21
24 yyy xxx 912,22
25 zzz fff 433,555,22
.. ... ... ...There are 200 million rows. There is maximum of about 10 IDs in the REFLIST, though typically two or three. How could I efficiently query this data on the REFLIST column? e.g. something like:
SELECT id FROM mytable WHERE :myval in reflistLogically there is a many to many relationship between rows in this table. The REFLIST column contains pointers to ID values elsewhere in the table. The data could be normalized so that the relationship keys are in a separate table (in fact this is the current solution that we want to change).
ID REF
21 24
21 25
21 78
21 412
22 21
24 912
... ...The comma separated list seems instinctively like a bad idea, however there are various reasons for proposing it. The main reason is because the source for this data has it structured like the REFLIST example. It is an OLTP-like system rather than a data warehouse. The source code (and edit performance) would benefit greatly from not having to maintain the relationship table as the data changes.
Going back to querying the REFLIST column, the problem seems to be building an approriate index for the data. The ideas proposed so far are:
<li>Make a materialized view that presents the relationships as normalized (e.g. as in the example with ID, REF columns above), then index the plain column - the various methods of writing the view SQL have been widely posted.
<li>Use a Oracle Text Index (not something I have ever had call to use before).
Any other ideas? Its Oracle 10.2, though 11g could be possible.
Thanks
JimSomething like this ?
This is test demo on my 11.2.0.1 Windows XP
SQL> create table test (id number,reflist varchar2(30));
Table created.
SQL> insert into test values (21,'24,25,78,412');
1 row created.
SQL> insert into test values (22,'21');
1 row created.
SQL> insert into test values (24,'912,22');
1 row created.
SQL> insert into test values (25,'433,555,22');
1 row created.
SQL> select * from test
2 where
3 ',' || reflist || ',' like '%,22,%';
ID REFLIST
24 912,22
25 433,555,22
SQL>Source:http://stackoverflow.com/questions/7212282/is-it-possible-to-query-a-comma-separated-column-for-a-specific-value
Regards
Girish Sharma
Edited by: Girish Sharma on Jul 12, 2012 2:31 PM -
Comma Separated Tags from BLT?
Was reading the help documentation and noticed that there is a way to pass in comma separated tags to a query template:
TagName
One or more tag names to be queried
String Or String List
None
The name of a tag in the underlying data source. This parameter can be repeated multiple times in a URL if multiple tags are to be queried, and multiple tags can be passed in a single parameter as a comma-separated list of tag names.
I tried this from a BLT and it didn't work. What is the trick to get this to work? I have about 50 tags to query and I dont want to hard code the mappings and I would rather not fire 50 individual queries.
Any thoughts?Brian,
You can't do it from an APPLET perspective or a standard TagQuery action block in BLS because of the explicit TagName.1 to TagName.128 query properties. The TagName property you found in the help only works for a direct query to the Illuminator servlet such as: http://<server:port>/Lighthammer/Illuminator?Server=Simulator&Mode=Current&TagName=L1Speed,L1Yield,AssetUtil1
If you need to wrap this functionality inside BLS or simply want to use it in an applet, then drop something like this into an XMLQuery template:
http://localhost/Lighthammer/Illuminator?QueryTemplate=Path/File&StartDate=[SD]&EndDate=[ED]&TagName=[Param.1]&Content-Type=text/xml
Assumptions would then be that you can bundle your data server and mode information in the query template (or you can keep them explicit with Server and Mode like in the first example and [Param.x] the right side of the equal sign to allow for simple Current/History/HistoryEvent changes). The [SD] and [ED] tokens will make the XMLQuery known to be time sensitive to an iChart so the default 60 minutes, etc. will go into the http request and give you the convenient VCR controls in the time control bar.
Regards,
Jeremy -
SQL Select with comma separated column value
Hi All
ASP VBScript
I have a DB column named allowed_contracts that stores a
comma separated
list of of values e.g. 3, 5, 19, 44, 52
I need to select records based on a variable called
varContractList that
contains another comma separated list i.e. 5, 44, 52
I only want to select records where the allowed_contracts
column contains
each of the varContractList,
For example only return records that have 5 or 44 or 52 in
thier
allowed_contracts column.
My brain is now in a persistant vegetive state trying to work
this out so
any ideas would be much appreciated.
Regards
BrenHi Jules
Cheers for this.
I was trying to be a bit cute (lazy even) by storing the
project ID's as a
comma delimted string but as we see it doesn't always pay to
be lazy. Serves
me right for destroying brain cells by drinking the Welshpool
beer the other
week whilst visiting mates down that neck of the woods. :-))
Time for another table me thinks.
Rgds
Bren
"Julian Roberts" <[email protected]> wrote in message
news:e7i13f$mup$[email protected]..
> Fatal flaw here Bren. In a relational database, one
shouldn't really store
> foreign keys as a comma delimted string. One should have
a 3 table
> structure. eg
>
> Products:
> ProductID
> Product
>
> Categories:
> CategoryID
> Category
>
> ProductCategories:
> ProductID
> CategoryID
>
> So, from the table ProductCategories, a product can
belong to many
> categories. When doing a front end search to find
products in multiple
> categories, products can be shown thus:
>
> select * from Products where ProductID in (select
ProductID from
> ProductCategories where CategoryID in (5,44))
>
> --
> Jules
>
http://www.charon.co.uk/charoncart
> Charon Cart 3
> Shopping Cart Extension for Dreamweaver MX/MX 2004
>
>
>
>
> -
Using Comma-Separated Selector
I am building a site with products that can have different colors.
Some products share colors.
I am using the Comma-Separated Selector to select the available colors for each product from a list.
My problem is showing each product and the colors available for each product.
How do I create a recordset that pulls only the colors I need from the colors table based on the comma separated feild from the products table?
Thanks for any help or point in the right direction.
RGNelsonDifficult to say without seeing how your database is planned
Nested region? - probably not
take a look at the MYSQL query with uses
HAVING IN (items in a comma separated list, item, item, etc)
this is the same approach as the article you mentioned
also a similar problem (possibly) mentioned here...
ryanpratt, "Multiple conditions in Querey builder Help" #2, 18 Nov 2008 7:30 am
atb
Paul -
Format Of LDAP Server List for Netmail
Could anyone please explain what the format for using an ldap server for address lookups in Netmail. ie:
servername:port/searchbase or ldap://servername:port/searchbase. I have tried a few but can't seem to get it to work. Everytime I try to add a server to this list, the Java Netmail will not allow me compose a message.. anyone have any ideas..Chris -
Each entry is a comma separated list of name/value pairs in the following format: name="value". Quotation marks are not allowed in any value. The valid names and corresponding preference are:
name the user-friendly name for the server; this is what the user sees in the NetMail Address Search tab.
server the host name of the LDAP server. If a port is needed, use host:port.
base the search base expression, e.g., ou=People
searchin the list of attributes to search in, e.g., cn,givenname
result the attribute to use as the result, defaults to mail
filter additional search filters to be applied
referral whether or not to follow referrals, true or false
Only the server value is required, all others are filled in with defaults if necessary. A typical entry might be:
name="Company Address Book", server="ourldap.xyz.com", base="dc=xyz,dc=com"
Stephen
Maybe you are looking for
-
IPad cannot be found after trying to update to 7.1.1
While updating my iPad to 7.1.1 through my computer, it crashed and now it says I need to restore, but it won't let me. I cant even touch the iPad, it shows the plug into iTunes icons. itunes is recognizing the device to some extent because when I pl
-
Hi Experts, Are account types different for business partner items and G/L items? While maintaining default values for repayent methods/payment medium in SPRO I am getting an error that my account is not a reconciliation account for FI-CA and it
-
Consecutive Numbering in New Documetns (Action)
Hi all, About two years ago I posted this query regarding a possible bug in Photoshop 4. As much as christoph pfaffenbich tried extremely hard to help me, he was unable to provide a solution. UNFORTUNATELY, the problem still exists in Photoshop CS
-
<blockquote>Locking duplicate thread.<br> Please continue here: [[/questions/900068]]</blockquote> The below drop down box with arrows do not work. I cannot tell you "This Happened" as the box just flickers. It will work in Explorer.
-
Implementing authentication using a token (by Vander)
Someone know how can I implement a Authentication in sigle-sign-on using a driver token?? thanks for some help