First, separate the comma-delimited field into a temporary table or table variable. That way, you can exactly or precisely join the strings. Create a row for each name and include a key column of a specific type that helps you adjust the rows.
The best way to do this is with a helper table:
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
result:
andy john mark
After you have two temporary tables, do a FULL OUTER JOIN and include the โfound in bothโ column with the given value. You will get a NULL value for names not found in both - and you can consider NULL as the value "False".
Can you indicate why you need to get a boolean for matches between two tables? What are you going to do next? Sometimes an explanation of this will lead to better solutions. You may find that you make haste assumptions. Best, Bill.
source share