473,412 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

SELECTing WHERE string is within other string

I have column data that looks like this:

id | ColumnA
---+-----------
1 | 12,35,123
2 | 1,23
3 | 233,34,35
4 | 34

And I want to be able to make a WHERE clause where I can match up a
value to one of the values between the commas. I.e. each of the numbers
between the commas are seperate values I wish to compare against.

Is there any function, perhaps like VBScripts "Split" function, where I
can split those numbers up and compare a value against them?

Perhaps there's another answer someone has?

Example:
If I'm searching with a value of "12" row 1 would be returned.
If I'm searching with a value of "35", row 1 and 3 would be returned.

--
[ Sugapablo ]
[ http://www.sugapablo.com <--music ]
[ http://www.sugapablo.net <--personal ]
[ su*******@12jabber.com <--jabber IM ]
Jul 20 '05 #1
4 5181
You're asking for a query solution to a problem caused by a flawed design.
Have you considered changing the design instead? This is much easier:

CREATE TABLE Sometable (id INTEGER, col_a INTEGER, PRIMARY KEY (id,col_a))

INSERT INTO Sometable VALUES (1,12)
INSERT INTO Sometable VALUES (1,35)
INSERT INTO Sometable VALUES (1,123)
INSERT INTO Sometable VALUES (2,1)
INSERT INTO Sometable VALUES (2,23)
INSERT INTO Sometable VALUES (3,233)
INSERT INTO Sometable VALUES (3,34)
INSERT INTO Sometable VALUES (3,35)
INSERT INTO Sometable VALUES (4,34)

SELECT id
FROM Sometable
WHERE col_a = 35

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2

"Sugapablo" <ru********@sugapablo.com> wrote in message
news:sl***********************@dell.sugapablo.net. ..
I have column data that looks like this:

id | ColumnA
---+-----------
1 | 12,35,123
2 | 1,23
3 | 233,34,35
4 | 34

And I want to be able to make a WHERE clause where I can match up a
value to one of the values between the commas. I.e. each of the numbers
between the commas are seperate values I wish to compare against.

Is there any function, perhaps like VBScripts "Split" function, where I
can split those numbers up and compare a value against them?

Perhaps there's another answer someone has?

Example:
If I'm searching with a value of "12" row 1 would be returned.
If I'm searching with a value of "35", row 1 and 3 would be returned.

--
[ Sugapablo ]
[ http://www.sugapablo.com <--music ]
[ http://www.sugapablo.net <--personal ]
[ su*******@12jabber.com <--jabber IM ]


There may well be a better way, but this should work (the string functions
in MSSQL are rather limited):

/* With a constant */

select *
from foo
where cola like '35,%' or
cola like '%,35,%' or
cola like '%,35'

/* With a variable */

declare @i int
set @i = 35

select *
from foo
where cola like cast(@i as varchar(5)) + ',%' or
cola like '%,' + cast(@i as varchar(5)) + ',%' or
cola like '%,' + cast(@i as varchar(5))

This will be inefficient on large data sets, though, and you may want to
look at your data model - lists in a single column can often be normalized.

Simon
Jul 20 '05 #3
As David pointed out, the design of your table makes this harder than it
needs to be. Best would be to change it, otherwise:

SELECT id, ColumnA
FROM tablename
WHERE ',' + ColumnA + ',' LIKE '%,35,%';

Hope that helps,
Rich
"Sugapablo" <ru********@sugapablo.com> wrote in message
news:sl***********************@dell.sugapablo.net. ..
I have column data that looks like this:

id | ColumnA
---+-----------
1 | 12,35,123
2 | 1,23
3 | 233,34,35
4 | 34

And I want to be able to make a WHERE clause where I can match up a
value to one of the values between the commas. I.e. each of the numbers
between the commas are seperate values I wish to compare against.

Is there any function, perhaps like VBScripts "Split" function, where I
can split those numbers up and compare a value against them?

Perhaps there's another answer someone has?

Example:
If I'm searching with a value of "12" row 1 would be returned.
If I'm searching with a value of "35", row 1 and 3 would be returned.

--
[ Sugapablo ]
[ http://www.sugapablo.com <--music ]
[ http://www.sugapablo.net <--personal ]
[ su*******@12jabber.com <--jabber IM ]

Jul 20 '05 #4
Sugapablo (ru********@sugapablo.com) writes:
I have column data that looks like this:

id | ColumnA
---+-----------
1 | 12,35,123
2 | 1,23
3 | 233,34,35
4 | 34

And I want to be able to make a WHERE clause where I can match up a
value to one of the values between the commas. I.e. each of the numbers
between the commas are seperate values I wish to compare against.

Is there any function, perhaps like VBScripts "Split" function, where I
can split those numbers up and compare a value against them?


As others have pointed out, this is not a good table design, and as a
collolary of this, there are not much functions in SQL Server to handle
this.

If you are stuck with this design, then you may get some ideas on
http://www.sommarskog.se/arrays-in-s...#unpack-tblcol, but you should
really consider the possibilities for a redesign.
--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon G Best | last post by:
Hello! The C++ standard library provides facilities for finding out the sizes (and other such stuff) of numeric types (::std::numeric_limits<>, for example). What I would like to do is to...
5
by: Axial | last post by:
Question: How to select columns from Excel-generated XML when some cells are empty. I've found examples where rows are to be selected, but I can't seem to extrapolate from that to selecting...
5
by: uthuras | last post by:
Machine : AIX 5.2 Product : UDB DB2 Release 8.1 FP4a I have problem loading data into destination table. The data file is huge with more than 6 Million records. This what i have done 1....
6
by: aaj | last post by:
Hi all I use a data adapter to read numerous tables in to a dataset. The dataset holds tables which in turn holds full details of the records i.e. keys, extra colums etc.. In some cases I...
1
by: sneha123 | last post by:
There will be some 20 questions and for each question there will be 4 choices.what i want to do is to select multiple answers by clicking the checkbox. i m using asp.net,vb.net pls help me we...
5
by: megahurtz | last post by:
I need to put together an SQL statement and I can't think of how to make it work properly. The scenario is that I have news items in a database that have a launch time and can optionally have an...
1
by: Tarik Monem | last post by:
Hi Everyone, I've been trying to do this application that I've been working on from so many different angels and I seem to be running into some kind of wall with every attempt. I have scrapped...
1
by: archcool | last post by:
I want to display new empty text fields within the same form after selecting an option from a combobox. here is the form Filter by:todays date,from date ---- in a combobox when from date is...
2
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.