January 15, 2014 at 11:21 am
Hi
I have TABLEA with a CSV column.
TABLEA[colId,colcsv]
TableA
----------
1, A,B,C,D
2, B,C
3, D,E,F
4, A,X,Y,Z,D
I have to retrieve ID 1,2 if the input is "B,C"
OR retrieve ID = 1,4 if the imput is "A,D".
Any ideas please?
Thanks.
January 15, 2014 at 11:33 am
January 15, 2014 at 11:39 am
Here's an idea. Notice that I'm including DDL and sample data as you should have done to get faster and better answers.
I'm using a function that you can find in this article http://www.sqlservercentral.com/articles/Tally+Table/72993/. Be sure to understand what it's doing.
CREATE TABLE #TABLEA(colId int,colcsv varchar(8000))
INSERT #TABLEA
SELECT
1, 'A,B,C,D' UNION ALL SELECT
2, 'B,C' UNION ALL SELECT
3, 'D,E,F' UNION ALL SELECT
4, 'A,X,Y,Z,D'
DECLARE @Valuevarchar(8000) = 'A,B';
SELECT a.colId
FROM #TABLEA a
CROSS APPLY dbo.DelimitedSplit8K( colcsv, ',') split
JOIN dbo.DelimitedSplit8K( @Value, ',') split2 ON split.Item = split2.Item
GROUP BY colId
HAVING COUNT(*) = (SELECT COUNT(*) FROM dbo.DelimitedSplit8K( @Value, ','))
DROP TABLE #TABLEA
Final note, try to normalize your data to avoid having this problems.
January 15, 2014 at 11:47 am
one CSV input has to be matched with CSV column for individual items
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply