Solved

SQL use SELECT to search for records from a field containing a list of number separated by commas

Posted on 2015-02-20
5
141 Views
Last Modified: 2015-02-20
We have a database that has a field in who contents are lots of strings of numbers separated by commas.

We want to do a select to extract records where this field contains one specific number.

eg

Example of this field could be "15,21,32,45,65,68"

And we want to select it using something like this:
- SELECT ID FROM myTable WHERE myField CONTAINS '21';

So it selects the record if the number exists anywhere in the field, whether it be on it's own or within a list of numbers.
0
Comment
Question by:Sean Clarke
5 Comments
 
LVL 11

Accepted Solution

by:
Dany Balian earned 500 total points
ID: 40621567
Select * from mytable where myfield like '%21%'
will give u what u want however it will also list records not containing 21 but actually containing 121 example: 17,121,34,36,45

If u don't want that then u change ur select to:

Select * from mytable where ','&myfield&',' like '%,21,%'
And it will only give u fields with 21 in it
0
 
LVL 11

Expert Comment

by:Dany Balian
ID: 40621578
Just one correction:
In access instead of % you use *
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 40621586
try

SELECT ID FROM myTable WHERE Instr("," & [myField] & ",", ",21,") >  0
0
 
LVL 1

Author Closing Comment

by:Sean Clarke
ID: 40621669
Nice one Dany, excellent solution - works a treat
0
 
LVL 34

Expert Comment

by:PatHartman
ID: 40621703
This is a poor design strategy since it violates first normal form and will prove difficult to work with.

To search the string, you would need to use the LIKE operator.  Using the LIKE operator in this case will force a full table scan.  No index can be used and no optimization is possible by the query engine.  Every single record in the table must be examined including every single character in the mushed field.  If you have more than a few thousand rows in this table, you will be verrrry unhappy with performance.

Where SomeField LIKE "*" & Forms!yourField & "*";

Keep in mind that embedded strings will cause confusion.  So if you search for 12, you'll get 12, 1244, 45124, etc.

I've attached a word document with the text of two articles (including links to the source) that show all the Access wild card options.
AccessWildCards.docx
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now