Reputation: 185
I am in need of a query (or stored procedure rather) for SQL Server wherein I need compare data of two different tables with the following structure
Table 1
ID Text Table2ID
1 Chris
2 John
3 Paul
Table2
ID Text
1 Mitchell Johnson
2 Chris Martin
3 Steven
By comparing the above two tables, I need to get the 'IDs' from table2 and insert them into the 'Table2ID' column of first table
Comparison criteria:-
The full word in 'Text' column of 'Table1' should be contained in 'Text' column of 'Table2'
In our case, we would obtain 'IDs' of first two rows from table2 (since the word John is contained in 'Mitchell Johnson' and the word Chris is contained in 'Chris Martin' and Paul is not contained in 'Table2')
How could I achieve this? It would be nice if someone out here is able to shed light on my path
Regards, Raghuraman.V
Upvotes: 2
Views: 19779
Reputation: 171351
The abstract answer is:
UPDATE Table
SET Table.col1 = other_table.col1
FROM Table
INNER JOIN other_table ON Table.id = other_table.id
Specifically:
update Table1
set Table1.Table2ID = Table2.ID
from Table1
inner join Table2 on Table2.Text like '%' + Table1.Text + '%'
Upvotes: 2