Solved

Select from comma delimited list previously saved in a char field

Posted on 2014-12-14
6
119 Views
Last Modified: 2014-12-14
I have a table with, among other fields, a varchar field named "Countries" that contains a comma delimited lists of country codes ( ie "28,76,78,98")
How can i select for instance all records where the id 78 is in that Countries list?

Thanks
Jaime
0
Comment
Question by:GreatSolutions
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40498824
where countries like '%[^0-9]78[^0-9]%'
0
 
LVL 2

Author Comment

by:GreatSolutions
ID: 40498846
Hi
It doesn't work
0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40498847
The worst possiible way to use SQL fields is to try storing compound data in them.  One field should contain one value and nothing more.  
Storing complex data like that slows your system down by massive amounts.  A linked table with two fields in it would have been your answer.

NewTable:CountryLinks
int ReferencID
int CountryCode

The referenceID is the key of the record that originally held your "Countries" field and CountryCode is each of the individual elements from your Counries field.

So assuming that your example above where you had  country codes ( "28,76,78,98") in a row with an ID field value of 129, you would have entries of....
ReferenceID  CountryCode
129                  28
129                  76
129                  78
129                  98

Now you can easily select anything in a normal, indexed, SELECT statement joining the two tables.
0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 2

Author Comment

by:GreatSolutions
ID: 40498848
I understand Neil but in this case i must use the field with the concat values in it...
0
 
LVL 37

Accepted Solution

by:
Neil Russell earned 500 total points
ID: 40498864
Without resulting to CLR then Off the top of my head your going to need to use the VERY slow...

SELECT * from tab1 where countries LIKE "78,%" OR countries LIKE "%,78" or countries LIKE "%,78,%"
0
 
LVL 2

Author Closing Comment

by:GreatSolutions
ID: 40498871
Changed the double quotes to single, and also added "or countries='78' for the case where there is only one country. Tested and it works correctly. I don't mind about speed, this is a table with a few hundred records at the most

Many thanks
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

730 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