1

The question above is all I need help with. I have two common separated lists and I need to compare the two lists and return the values that are in both lists.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TheSQLLearner
  • 51
  • 1
  • 8
  • 1
    Normalize the data properly, then just use an `INNER JOIN`. – David Jan 26 '16 at 15:46
  • Agree with @David. I take it you have a list of values in a single field, in your SQL database. That is not proper normalized form. You need to put the values in a separate table. Once you have a properly normalized db, then you can expect SQL to do the comparison for you. –  Jan 26 '16 at 15:51

2 Answers2

0

You could, for instance, parse each list into a table (variable) and then use an inner join to obtain the common values.

cdonner
  • 37,019
  • 22
  • 105
  • 153
0

You can use the Split function from this SO post and then get the common elements using INTERSECT:

DECLARE @String1 VARCHAR(50)='A,B,C'
DECLARE @String2 VARCHAR(50)='A,B'

SELECT  * FROM dbo.Split(@String1, ',')
INTERSECT
SELECT  * FROM dbo.Split(@String2, ',')

I am referring to this Split function as originally posted by Romil Kumar Jain here.

CREATE FUNCTION Split (
      @InputString                  VARCHAR(8000),
      @Delimiter                    VARCHAR(50)
)

RETURNS @Items TABLE (
      Item                          VARCHAR(8000)
)

AS
BEGIN
      IF @Delimiter = ' '
      BEGIN
            SET @Delimiter = ','
            SET @InputString = REPLACE(@InputString, ' ', @Delimiter)
      END

      IF (@Delimiter IS NULL OR @Delimiter = '')
            SET @Delimiter = ','

--INSERT INTO @Items VALUES (@Delimiter) -- Diagnostic
--INSERT INTO @Items VALUES (@InputString) -- Diagnostic

      DECLARE @Item                 VARCHAR(8000)
      DECLARE @ItemList       VARCHAR(8000)
      DECLARE @DelimIndex     INT

      SET @ItemList = @InputString
      SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
      WHILE (@DelimIndex != 0)
      BEGIN
            SET @Item = SUBSTRING(@ItemList, 0, @DelimIndex)
            INSERT INTO @Items VALUES (@Item)

            -- Set @ItemList = @ItemList minus one less item
            SET @ItemList = SUBSTRING(@ItemList, @DelimIndex+1, LEN(@ItemList)-@DelimIndex)
            SET @DelimIndex = CHARINDEX(@Delimiter, @ItemList, 0)
      END -- End WHILE

      IF @Item IS NOT NULL -- At least one delimiter was encountered in @InputString
      BEGIN
            SET @Item = @ItemList
            INSERT INTO @Items VALUES (@Item)
      END

      -- No delimiters were encountered in @InputString, so just return @InputString
      ELSE INSERT INTO @Items VALUES (@InputString)

      RETURN

END -- End Function
GO
Community
  • 1
  • 1
Alex
  • 21,273
  • 10
  • 61
  • 73
  • The strings have already been created and are stored in a variable... How would you then manipulate the code to solve the problem. – TheSQLLearner Jan 26 '16 at 15:56
  • Updated post, strings are now stored in variables called `@String1` and `@String2` – Alex Jan 26 '16 at 16:03
  • All I could find on this website was how to return true, false, 1 or 0 if values matched... Not the actual values themselves. – TheSQLLearner Jan 26 '16 at 16:22