Hello I have 2 tables One Eligibility and one Hospital. The Eligibility table has the last name , first name in separate columns and Hospital has them both in one column with a comma in between. I want to join if the names are equal to each other.
This is failing
Select H.Name,M.MemberLastname + ','+ M.MemberFirstName as Names
from Hospital h inner join Eligibility M on H.name = Names
You cannot join on the column name in that manner - you can use a derived table or CTE...or you put the calculation in the join expression:
SELECT ...
FROM Hospital h
INNER JOIN (SELECT *
, m1.MemberLastName + ',' + m.MemberFirstName As Names
FROM Eligibility m1) m ON m.Names = h.name
SELECT ...
FROM Hospital h
INNER JOIN Eligibility m ON m.MemberLastName + ',' + m.MemberfirstName = h.name
JOINing on a concatenated string will run like a dog If the number of rows you have is small it won't be a problem ... unless the number of rows will grow/scale (over time).
Its fragile. Are the names really maintained absolutely identically in both tables? No ambiguity with "John Smith" etc?
I always prefer to see some sort of ID in these cases, so that the Name itself is not the key. Typos get fixed, People change name on marriage and so on ... all sorts of pitfalls.
Hopefully none of that applies in your case, but just thought I'd raise it ... just in case