The first parameter, CHARINDEX (or PATINDEX), must be a substring of the second parameter. None of the functions are smart enough to match a specific part of a substring. Rice / Bennet cannot be found in Rice 02. Examples:
CHARINDEX('Miller', '01 Miller') = 4 CHARINDEX('Grant', '03 Grant') = 4 CHARINDEX('Rice/Bennet', '02 Rice') = 0
To make the query work, you will need to build an inline view in which you will parse the value of Table1.Column to deal with these situations. CHARINDEX / PATINDEX reports that you are using SQL Server - if it is 2005+, you can use Common Table Expressions (CTE).
JOIN (SELECT CASE WHEN CHARINDEX('/', t.column) > 0 THEN SUBSTRING(t.column, 0, CHARINDEX('/', t.column)-1) ELSE t.column END AS column FROM TABLE1 t) t1 ON CHARINDEX(t1.column, Table2.column) > 0
Remember that in the example, βBennetβ will never be used to check the corresponding entry in table2.
source share