I have two tables:
table "Person"
ID FirstName LastName
----------- ---------- ----------
1 Janez Novak
2 Matija Špacapan
3 Francka Joras
Table "UserList"
ID FullName
----- --------------------
1 Andrej Novak
2 Novak Peter Janez
3 Jana Novak
4 Andrej Kosir
5 Jan Balon
6 Francka Joras
7 France Joras
As a result, the query must return those IDs from both tables, that FirstName and Lastname from table Person exist in table UserList. The name and Lastname must be precisely the same. FullName in table UserList can include the middle name - which should be "ignored".
Match: Janez Novak = Janez Novak OR Novak Janez OR Janez Peter Novak
Not a match: Janez Novak <> Janeza Novak OR Jjanez Novak
Wanted results:
ID FirstName LastName ID WholeName
---- ---------- --------- ---- -------------------
1 Janez Novak 2 Novak Peter Janez
3 Francka Joras 6 Francka Joras
This is my query:
SELECT
A.ID
,A.FirstName
,A.LastName
,B.ID
,B.WholeName
FROM
dbo.UserList B
cross join dbo.Person A
WHERE
(
CHARINDEX('"'+A.FirstName+'"', '"'+Replace(B.WholeName,' ','"')+'"') > 0
AND CHARINDEX('"'+A.LastName+'"', '"'+Replace(B.WholeName,' ','"')+'"') > 0
)
The query works OK when there are not many records in the tables.
But my tables have: "Person" -> 400k and "UserList" -> 14k records.
Is my approach to finding a solution OK, or is there any other more efficient way to do that? Thank you.
BR