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
143 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 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 35

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How To Convert Date Stored as Varchar as an MSSQL DATE Type Field In a VIEW 3 23
VB.net and sql server 4 36
SQL Error - Query 6 26
SQL Group on First occurrence 9 25
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

809 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