473,654 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with a query: returning rows from a coma delimited list of integers...

3 New Member
Hi, I'm rather new to writing queries, so please forgive me if this is a really simple thing to do.

I need to create a query that finds out which rows in the DB have ANY of the values that are in a given list. Only one column in each row of the DB has their values stored in a list.

Example:

Passed list = 2,5,10,6

ColumX data in database:
Row 1 = 3, 5, 21, 4
Row 2 = 1, 4, 7, 9
Row 3 = 2
Row 4 = 8, 10, 3, 6
Row 5 = 1, 22, 9, 4

Base on the passed list, the query should return the following rows:
Row 1 (because 5 was in the list)
Row 3 (because 2 was in the list)
Row 4 (because 10 and 6 was in the list)

I’m hoping threes some SQL function that works with lists and can compare the data sets of two lists to find matching values, but I don’t know.

Can anyone help?
thanks
Jul 15 '07 #1
3 1767
Infide
28 New Member
Hi, I'm rather new to writing queries, so please forgive me if this is a really simple thing to do.

I need to create a query that finds out which rows in the DB have ANY of the values that are in a given list. Only one column in each row of the DB has their values stored in a list.

Example:

Passed list = 2,5,10,6

ColumX data in database:
Row 1 = 3, 5, 21, 4
Row 2 = 1, 4, 7, 9
Row 3 = 2
Row 4 = 8, 10, 3, 6
Row 5 = 1, 22, 9, 4

Base on the passed list, the query should return the following rows:
Row 1 (because 5 was in the list)
Row 3 (because 2 was in the list)
Row 4 (because 10 and 6 was in the list)

I’m hoping threes some SQL function that works with lists and can compare the data sets of two lists to find matching values, but I don’t know.

Can anyone help?
thanks
You might get stuck with having to use a cursor.

This should get you started.
Expand|Select|Wrap|Line Numbers
  1. DECLARE @PassedList VARCHAR(5000)
  2. DECLARE @ParsedList VARCHAR(5000)
  3. DECLARE @ROW VARCHAR(5000)
  4. set @passedlist = '2,5,10,6'
  5.  
  6. declare @ParsedInt int
  7. declare @CompareInt int
  8.  
  9.  
  10.  
  11. DECLARE  C cursor  LOCAL FAST_FORWARD FOR
  12. SELECT  ColumnX
  13.     From TableTest
  14.  
  15. OPEN C
  16.  
  17. FETCH NEXT FROM C INTO @Row
  18.  
  19. WHILE @@Fetch_Status = 0 
  20. BEGIN
  21.  
  22. SET @ParsedList = @PassedList
  23. while len(@ParsedList) > 0
  24. BEGIN
  25.     IF charindex(',',@ParsedList) > 0
  26.     BEGIN
  27.     SET @ParsedInt = substring(@ParsedList,0,charindex(',',@ParsedList))
  28.     SET @ParsedList = substring(@passedlist,charindex(',',@ParsedList) + 1, len(@ParsedList))
  29.     END
  30.     ELSE
  31.     BEGIN
  32.     SET @ParsedInt = @ParsedList
  33.     SET @ParsedList = ''
  34.     END
  35.  
  36.     WHILE LEN(@Row) > 0--compare every comma delimited value in row to the parsed int from the passed list
  37.     BEGIN
  38.         IF charindex(',',@Row) > 0
  39.         BEGIN
  40.         SET @CompareInt = substring(@Row,0,charindex(',',@Row))
  41.         SET @Row = substring(@Row,charindex(',',@Row) + 1, len(@Row))
  42.         END
  43.         ELSE
  44.         BEGIN
  45.         SET @CompareInt = @Row
  46.         SET @Row = ''
  47.         END
  48.  
  49.         IF (@CompareInt = @ParsedInt)
  50.         BEGIN
  51.             --DO SOMETHING
  52.             print 'Found a match'
  53.         END
  54.  
  55.  
  56.     END--end row parse row loop
  57. END--end parse passedlist loop
  58.  
  59.  
  60.  
  61. FETCH NEXT FROM C INTO @Row
  62.  
  63. END
  64.  
  65.  
Jul 15 '07 #2
Frank11
3 New Member
Infide

Thanks for the reply, but dam, I've never seen SQL code like that. Looks more like a program in C than a query.
I copy and pasted what you wrote into my query (its being sent within a Cold Fusion query) and the system doesn't like it. No error, but the query name that was given in the CF tag doens't is not available for the Output.

Would there be a simpler way you think to compare the values of these to lists?

cheers
F.






You might get stuck with having to use a cursor.

This should get you started.
Expand|Select|Wrap|Line Numbers
  1. DECLARE @PassedList VARCHAR(5000)
  2. DECLARE @ParsedList VARCHAR(5000)
  3. DECLARE @ROW VARCHAR(5000)
  4. set @passedlist = '2,5,10,6'
  5.  
  6. declare @ParsedInt int
  7. declare @CompareInt int
  8.  
  9.  
  10.  
  11. DECLARE  C cursor  LOCAL FAST_FORWARD FOR
  12. SELECT  ColumnX
  13.     From TableTest
  14.  
  15. OPEN C
  16.  
  17. FETCH NEXT FROM C INTO @Row
  18.  
  19. WHILE @@Fetch_Status = 0 
  20. BEGIN
  21.  
  22. SET @ParsedList = @PassedList
  23. while len(@ParsedList) > 0
  24. BEGIN
  25.     IF charindex(',',@ParsedList) > 0
  26.     BEGIN
  27.     SET @ParsedInt = substring(@ParsedList,0,charindex(',',@ParsedList))
  28.     SET @ParsedList = substring(@passedlist,charindex(',',@ParsedList) + 1, len(@ParsedList))
  29.     END
  30.     ELSE
  31.     BEGIN
  32.     SET @ParsedInt = @ParsedList
  33.     SET @ParsedList = ''
  34.     END
  35.  
  36.     WHILE LEN(@Row) > 0--compare every comma delimited value in row to the parsed int from the passed list
  37.     BEGIN
  38.         IF charindex(',',@Row) > 0
  39.         BEGIN
  40.         SET @CompareInt = substring(@Row,0,charindex(',',@Row))
  41.         SET @Row = substring(@Row,charindex(',',@Row) + 1, len(@Row))
  42.         END
  43.         ELSE
  44.         BEGIN
  45.         SET @CompareInt = @Row
  46.         SET @Row = ''
  47.         END
  48.  
  49.         IF (@CompareInt = @ParsedInt)
  50.         BEGIN
  51.             --DO SOMETHING
  52.             print 'Found a match'
  53.         END
  54.  
  55.  
  56.     END--end row parse row loop
  57. END--end parse passedlist loop
  58.  
  59.  
  60.  
  61. FETCH NEXT FROM C INTO @Row
  62.  
  63. END
  64.  
  65.  
Jul 15 '07 #3
Vidhura
99 New Member
Infide

Thanks for the reply, but dam, I've never seen SQL code like that. Looks more like a program in C than a query.
I copy and pasted what you wrote into my query (its being sent within a Cold Fusion query) and the system doesn't like it. No error, but the query name that was given in the CF tag doens't is not available for the Output.

Would there be a simpler way you think to compare the values of these to lists?

cheers
F.
Refer this

http://weblogs.sqlteam.com/dinakar/a.../28/60150.aspx
Jul 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3315
by: Funnyweb | last post by:
I have a database table, which has field that could contain a single integer or a list of comma separated integers. Is it possible to match each row of that field against an array of integers and return those rows where any of the integers in that field are in my array? For example suppose I did the following: arInts = array(2,4,6,8,10);
3
1553
by: waters | last post by:
I seem to have hit a snag. I am trying to add the list (l_local) as an item in the output list (l_output). "l_local" is a modified copy of the format-list "l_format", which will be updated by index position within the function. The problem occurs in the following statement block: def create_apid_list(l_format,l_values): l_output = for t_curr_line in l_values: # ...for each list in the values list
3
5216
by: Paul Mateer | last post by:
Hi, I have been running some queries against a table in a my database and have noted an odd (at least it seems odd to me) performance issue. The table has approximately 5 million rows and includes the following columns: DocID (INTEGER, PRIMARY KEY, CLUSTERED) IsRecord (INTEGER, NONCLUSTERED)
6
4839
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID SalesManName AT Alan Time
8
2736
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only the sequence nos and it should be such that i can access it through the structure .plz help me .
4
1849
by: Hemant Shah | last post by:
Folks, I am having problem with an application that uses static SQL, the application basically browses through the table given start and end key most of the time it is processed from begining to end. The db2expln tells me that there is no Data or Index prefetch. I am running DB2 UDB 8 on AIX 5.3, and I am using DMS tablespace on raw logical volumes on a SAN.
1
2050
by: trint | last post by:
Hi, I have a dataGrid that when I try to add a row, I can't keep the data separated into columns. Here is what I tried to separate the columns (a coma): string completeProductsString = new string; DataGridViewRowCollection rows = this.dataGridView1.Rows; completeProductsString = productsString0 + "," + productsString1; rows.Add(completeProductsString);
1
2126
by: Frank11 | last post by:
Hi, I'm rather new to writing queries, so please forgive me if this is a really simple thing to do. I need to create a CF query that finds out which rows in the DB have ANY of the values that are in a given list. Only one column in each row of the DB has their values stored in a list. Example: Passed list = 2,5,10,6 ColumX data in database: Row 1 = 3, 5, 21, 4
1
2928
by: vikjohn | last post by:
I have a new perl script sent to me which is a revision of the one I am currently running. The permissions are the same on each, the paths are correct but I am getting the infamous : The specified CGI application misbehaved by not returning a complete set of HTTP headers. The scripts are very long but here are the opening statements: The One that works .... #!C:\Perl\bin\perl.exe # openresolver.cgi # # OpenResolver - a CGI script for...
0
8376
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8594
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7307
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.