[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.sqlserver.programming

Referring to comma separated values within a field

lesleyann76

3/30/2007 1:38:00 AM

In sql, in a given column I have records with information in them
which is separated by commas (ie, 1,2,5,8,12) or (KD, K, DF) etc., yet
it is all contained in one field. Well ordinarily when I evaluate the
contents of a field I am familiar with referring to the object.column
and generally looking at the contents of the located field as a
whole. When I have those fields in which multiple items of
information, separated by commas are all contained in one field how to
I refer to them. For instance, I could :
SELECT [ProductionLineNumber]
FROM ProductionTable
HAVING [ProductionLineNumber] = 8;

But if a targeted field in [ProductionLineNumber] contains 1,2,5,8,12
in its record it will return nothing because I don't know how to refer
to each of the items independently as they are grouped in the same
cell.

1 Answer

Aaron [SQL Server MVP]

3/30/2007 1:52:00 AM

0

There are no "cells" in SQL Server, you can't use HAVING instead of WHERE
like that, and you shouldn't be storing a comma-separated list when you
actually care about individual items within that list. Kind of like making
a salad when you only want to eat a tomato slice.

Anyway, you can try something like this, but performance is going to be
horrible. You really should reconsider the design:

WHERE ',' + REPLACE(ProductionLineNumber, ' ', '') + ',' LIKE '%,8,%'

--
Aaron Bertrand
SQL Server MVP
http://www.sq...
http://www.aspfa...



<lesleyann76@gmail.com> wrote in message
news:1175218668.063522.236930@n59g2000hsh.googlegroups.com...
> In sql, in a given column I have records with information in them
> which is separated by commas (ie, 1,2,5,8,12) or (KD, K, DF) etc., yet
> it is all contained in one field. Well ordinarily when I evaluate the
> contents of a field I am familiar with referring to the object.column
> and generally looking at the contents of the located field as a
> whole. When I have those fields in which multiple items of
> information, separated by commas are all contained in one field how to
> I refer to them. For instance, I could :
> SELECT [ProductionLineNumber]
> FROM ProductionTable
> HAVING [ProductionLineNumber] = 8;
>
> But if a targeted field in [ProductionLineNumber] contains 1,2,5,8,12
> in its record it will return nothing because I don't know how to refer
> to each of the items independently as they are grouped in the same
> cell.
>