1

I have two columns with comma separated values, want to find out if all the coma separated elements from col1 are present in col2 or not.

For example

col1        | col2
========================= 
AB          | AA,BB
BC          | CC,DD,BC
AB,BC       | AB,AA,BB,BC
CD,EF,GH    | AA,BB,CD,EF

This should give me the following results

col1        | col2          | Col1 Elements in Col2?
==================================================== 
AB          | AA,BB         | N
BC          | CC,DD,BC      | Y
AB,BC       | AB,AA,BB,BC   | Y
CD,EF,GH    | AA,BB,CD,EF   | N
SVaidya
  • 167
  • 2
  • 10
  • you first need a csv parser. do you already have a function to do that? – Quark Jun 14 '17 at 19:52
  • What version of SQL Server? 2016? – mjw Jun 14 '17 at 19:53
  • 3
    If at all possible you need to fix the design, not hack a query for this. You are violating 1NF by storing multiple values in a single tuple. This causes all sorts of problems and makes your life a LOT harder than it needs to be. – Sean Lange Jun 14 '17 at 20:02
  • @mjw We are using SQL Server 2012. – SVaidya Jun 14 '17 at 20:06
  • To accomplish this you're most likely going to need a split function, which doesn't exist until SQL Server 2016. You can find many examples on SO and elsewhere. I agree with Sean though, your data is making your life harder than it needs to be. https://stackoverflow.com/questions/23438761/split-comma-delimited-string-and-insert-to-a-table-int – mjw Jun 14 '17 at 20:07

0 Answers0