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
137 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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.

744 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

11 Experts available now in Live!

Get 1:1 Help Now