Solved

Use SQL to find string within database field

Posted on 2011-02-28
18
1,401 Views
Last Modified: 2012-05-11
I have the following values stored in a comma delimited list... for example

list: 13,14,16,18

there is a url variable #fc# that i need to find in the database field containing this list (named teams_selected).  I tried to write a query to do as described but it doesn't seem to be working.

<cfquery>select * from teams_schedule_new where '#fc#' in (teams_selected) order by id desc</cfquery>

Your help is appreciated.
0
Comment
Question by:Eduski
[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
  • 8
  • 8
  • 2
18 Comments
 
LVL 19

Expert Comment

by:Jones911
ID: 35003777
what is teams_selected ?
0
 
LVL 3

Author Comment

by:Eduski
ID: 35003790
it is the database field that contains the list of comma separated items

value is "13,14,16,18"
0
 
LVL 19

Expert Comment

by:Jones911
ID: 35003792
is fc a integer?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 3

Author Comment

by:Eduski
ID: 35003805
doesn't have to be.  the query as written just returns no records if i test it
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 35003825
The problem is that the variable "teams_selected" is a string, NOT a comma delimited list of integers.  In order to use the IN construct, you need a COMMA DELIMITED list of values - you need to break the variable teams_selected into its set of separate values, as a collection (the separate values 13, 14, 16 and 18 as individual values.

check out

http://www.sqlbook.com/SQL/Parse-a-comma-delimited-list-8.aspx

which does precisely what you need.

AW
0
 
LVL 19

Expert Comment

by:Jones911
ID: 35003841
Author states teams_selected is a list.

Author is teams_seletced meant to be in ## so as to be evaluated as a variable?
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 35003848
you can also use BING, and query on

sql parse comma delimited string

AW
0
 
LVL 3

Author Comment

by:Eduski
ID: 35003852
Arthur is right.  teams_selected is right now a string, which is where I was going wrong.  Since I can't understand the link Arthur posted, can someone tell me how to convert the string value to a list so I can properly search it?
0
 
LVL 3

Author Comment

by:Eduski
ID: 35003858
Sample code is appreciated, I'm not good at SQL.
0
 
LVL 19

Expert Comment

by:Jones911
ID: 35003886
select * from teams_schedule_new where teams_selected like '%,#fc#,%' order by id desc

should do the same thing
0
 
LVL 3

Author Comment

by:Eduski
ID: 35003903
yeah but the last number isn't followed by a comma
0
 
LVL 3

Author Comment

by:Eduski
ID: 35003910
and the first isn't preceeded by a comma either
0
 
LVL 19

Expert Comment

by:Jones911
ID: 35003914
select *
from teams_schedule_new
where teams_selected like '%,#fc#,%'
or ','+teams_selected like '%,#fc#,%'
or teams_selected+',' like '%,#fc#,%'
order by id desc
0
 
LVL 3

Author Comment

by:Eduski
ID: 35003929
your last suggestion eliminates records where '13' is the only entry without commas.
0
 
LVL 19

Expert Comment

by:Jones911
ID: 35003934
select *
from teams_schedule_new
where teams_selected like '%,#fc#,%'
or ','+teams_selected like '%,#fc#,%'
or teams_selected+',' like '%,#fc#,%'
or eams_selected = '#fc#'
order by id desc
0
 
LVL 19

Accepted Solution

by:
Jones911 earned 500 total points
ID: 35003936
eams_selected  in the 2nd last line needs to be teams_selected
0
 
LVL 3

Author Comment

by:Eduski
ID: 35003940
its not pretty but it works, thanks.
0
 
LVL 19

Expert Comment

by:Jones911
ID: 35003953
Yeah.  Gotta do what you gotta do when the database is not designed right though :)  Glad it worked.
0

Featured Post

Upcoming Webinar: Securing your MySQL/MariaDB data

Join Percona’s Chief Evangelist, Colin Charles as he presents Securing your MySQL®/MariaDB® data on Tuesday, July 11, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

717 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