You have them the wrong way around:
select charindex('bcd','abcde')
gives 2
select charindex('abcde','bcd')
gives 0
Rob
You have them the wrong way around:
select charindex('bcd','abcde')
gives 2
select charindex('abcde','bcd')
gives 0
Rob
The first parameter of CHARINDEX (or PATINDEX) must be a substring of the second parameter. Neither function is smart enough to match on a specific portion of the substring. 'Rice/Bennet' can't be found in '02 Rice'. Examples:
CHARINDEX('Miller', '01 Miller') = 4
CHARINDEX('Grant', '03 Grant') = 4
CHARINDEX('Rice/Bennet', '02 Rice') = 0
In order to make the query work, you'd need to construct an inline view where you parse the Table1.Column value to deal with these situations. CHARINDEX/PATINDEX tells us you're using SQL Server - if it is 2005+, you can use Common Table Expressions (CTEs).
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
Mind that in the example, 'Bennet' wouldn't ever be used to check for a corresponding entry in Table2.
Based on the update to your question if you want to trim off the first few characters including the space in the 2nd column and compare that to the 1st column like you described then this is what you can stick down in your WHERE clause.
(CHARINDEX(RIGHT(table2.column, len(table2.column)-CHARINDEX(' ', table2.column)), table1.column) > 0)