Solved

SQL Question

Posted on 2015-02-11
5
106 Views
Last Modified: 2015-02-12
Hello,

I have a SQL Server table with field (let's call it "Categories") that contains a list of IDs (e.g.: 1,3,5,11)

I want to find all rows where Categories contains the number 1 (but not 11)

Is this possible in standard SQL and, if so, how?

Thanks in advance.
0
Comment
Question by:skbohler
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40603359
You mean really the number 1 (unit) and not any number with the digit 1 (10...19, 21, 31, ..., 100..199, ...)?
Also, define "list of IDs". It's a string with numbers separated by commas?
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40603627
skbohler, Vitor is good enough with SQL that I think he'll get you a solution. (Points to him, not me :)  

But PLEASE, before you DREAM of designing another table with a comma-delimited list, read up on First Normal Form!  You'll save yourself all kinds of troubles like this!
0
 

Author Comment

by:skbohler
ID: 40603635
Daniel,

Yeah, I wish the previous developer hadn't built the DB with the comma-delimited list.
0
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 40603646
Ok, understood.  I've inherited some horrible code in my time too. ;)
0
 
LVL 48

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40605092
So, it's really a comma-delimited. If you want to find only the number 1 and assuming that the list is ordered then you can use the following code:
SELECT *
FROM Categories
WHERE CatID='1' OR CatID LIKE '1,%'

Open in new window


Just in case of the list isn't ordered then use this code:
SELECT *
FROM Categories
WHERE CatID='1' OR CatID LIKE '1,%' OR CatID LIKE '%,1,%' OR CatID LIKE '%,1'

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

828 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