Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 11

Accepted Solution

by:
Dany Balian earned 2000 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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 39

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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.

618 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