Separate the comma-delimited field first into a temporary table or table variable. That way, you can join or match rows accurately. Make a row for each name and include a key column of some sort that will help you correlate the rows.
The best way to do this is with a "helper table" like so:
DECLARE @numbers TABLE (number int)
DECLARE @i int
SET @i = 1
WHILE (@i < 1001)
BEGIN
INSERT INTO @numbers (number) VALUES (@i)
SET @i = @i+1
END
DECLARE @TestString VARCHAR(200)
SET @TestString = 'andy,john,mark'
DECLARE @RowDelimiter VARCHAR(1)
SET @RowDelimiter=','
SELECT SUBSTRING(@TestString+@RowDelimiter, number,
CHARINDEX(@RowDelimiter, @TestString+@RowDelimiter, number) - number)
FROM @numbers
WHERE number <= LEN(@TestString)
AND SUBSTRING(@RowDelimiter+ @TestString, number, 1) = @RowDelimiter
ORDER BY number
-- helper table technique: [email protected]
the result is:
andy
john
mark
Once you have the two temporary tables, then do a FULL OUTER JOIN and include your "found in both" column with a set value. You'll get the NULL value for names not found in both - and you can treat the NULL as the "False" value.
Can you mention why you need to get a boolean value for matches between the two tables? What are you going to do with it next? Sometimes explaining that will lead to better solutions. You might find that you are making assumptions hastily. Best, Bill.