Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 tables
Table1:
======
ID Name EmpID
== ==== =====
1 Dan 12
2 Isaro 13

Table2:
======
ID Name EmpID
== ==== ======
1 Muhi Dan NULL
2 Johanna Isaro NULL

I want to insert into table2 the EmpID from table1 but I want to check if Dan, Isaro from the table 1 exist in the table2. If so then it can insert into table2 the same EmpID from table1.
As u can see in the table2 there is muhi Dan means Dan exists in the table2, means then it should insert this.
How to perform this?
Posted

1 solution

SQL
UPDATE Table2
SET EmpID = t1.EmpID
FROM
    Table2 t2
INNER JOIN
    Tabel1 t1
ON 
    t2.Name LIKE '%' + t1.Name

This makes sure that the name in table2 ends with the name in table1.
 
Share this answer
 
Comments
Gupta Poonam 5-Sep-14 9:44am    
Solution will give a wrong result if substring of other name is same as the name you are looking for eg: if 'Jordan' name exist in table instead of dan it will still give you the output
kbrandwijk 5-Sep-14 11:01am    
You are right. Insert a whitespace before the Name: LIKE '% ' + t1.Name to only get full last names.
El Dev 5-Sep-14 10:04am    
thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900