Solved

Select from comma delimited list previously saved in a char field

Posted on 2014-12-14
6
111 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:SimonAdept
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

762 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

21 Experts available now in Live!

Get 1:1 Help Now