Click here to Skip to main content
15,888,162 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
In SQL Server how can i compare comma separated string values in a table column with parameter value as comma separated


SQL
Product Dimension
1       1,3,5
2       1,3,7

sql stored procedure parameter i'll pass as 1,3,5 or 1,5,3 or any order of mentioned numbers combination

i need to check whether passed value available in table or not?
Posted
Comments
Vedat Ozan Oner 3-Mar-14 2:05am    
what have you done? Where did you get stuck?
HarisJayadev 3-Mar-14 2:55am    
i don know how to compare comma separated values using loop in sql server stored procedure. is it good idea to use loop or do i have any other option
Maciej Los 3-Mar-14 2:39am    
Do you want to compare Product or Dimension (no matter of order of numbers in a string variable)?
HarisJayadev 3-Mar-14 2:55am    
I want to compare dimension with comma separated values
Maciej Los 3-Mar-14 2:56am    
Please, read my question carefully and answer it ;)

1 solution

Please, read my comment to the question.

If youo want to compare Dimensions, all you need to to is:

  1. parse Dimension for each product

    Product ValueOfDimension
    1 1
    1 3
    1 5
    2 1
    2 3
    2 7

  2. parse Dimension from string variable
  3. compare them


If you provide more details, i'll promise to update my answer.

[EDIT]
Have a look here:

SQL
DECLARE @tmp TABLE (Product INT IDENTITY(1,1), Dimension VARCHAR(30))

INSERT INTO @tmp (Dimension)
VALUES('1,3,5'),
('1,3,7')

DECLARE @input VARCHAR(30) = '1,5,3'

;WITH FirstCTE AS
(
	SELECT Product, CONVERT(INT, LEFT(Dimension, CHARINDEX(',', Dimension)-1)) AS MyValue, RIGHT(Dimension , LEN(Dimension) - CHARINDEX(',', Dimension)) AS Remainder
	FROM @tmp
	WHERE CHARINDEX(',', Dimension)>0
	UNION ALL
	SELECT Product, CONVERT(INT, LEFT(Remainder, CHARINDEX(',', Remainder)-1)) AS MyValue, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(',', Remainder)) AS Remainder
	FROM FirstCTE
	WHERE CHARINDEX(',', Remainder)>0
	UNION ALL
	SELECT Product, CONVERT(INT, Remainder) AS MyValue, NULL AS Remainder
	FROM FirstCTE
	WHERE CHARINDEX(',', Remainder)=0
),
SecondCTE AS
(
	SELECT CONVERT(INT, LEFT(@input, CHARINDEX(',', @input)-1)) AS MyValue, RIGHT(@input, LEN(@input) - CHARINDEX(',', @input)) AS Remainder
	WHERE CHARINDEX(',', @input)>0
	UNION ALL
	SELECT CONVERT(INT, LEFT(Remainder, CHARINDEX(',', Remainder)-1)) AS MyValue, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(',', Remainder)) AS Remainder
	FROM SecondCTE
	WHERE CHARINDEX(',', Remainder)>0
	UNION ALL
	SELECT CONVERT(INT, Remainder) AS MyValue, NULL AS Remainder
	FROM SecondCTE
	WHERE CHARINDEX(',', Remainder)=0
)
SELECT t1.Product, t1.MyValue AS MyValue1, t1.Remainder AS Remainder1, t2.MyValue AS MyValue2, t2.Remainder AS Remainder2 
FROM FirstCTE AS t1 INNER JOIN  SecondCTE AS t2 ON t1.MyValue = t2.MyValue 


As you can see, i used CTE[^].
[/EDIT]
 
Share this answer
 
v2
Comments
HarisJayadev 3-Mar-14 3:05am    
ya i have converted my table as us mentioned in above answer. I need to compare as no matter of order of numbers. for example if i pass stored procedure parameter as '1,3,5' or '1,5,3' or any 3*3=9 combination. it should tell as dimension combination exist for a product.
HarisJayadev 3-Mar-14 4:08am    
Product ID will repeat for combination of dimension like
Product Dimension
1 1
1 3
1 5

For Example i'm passing comma separated values as 1,3,5 then it shoulod return value available
if is end values as 1,5,4 it should return value not available
if i send value as 5,1,3 or 1,5,3 or 3,5,1 like this it should return values as value available

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900