Searching for Records With First And Last Names

I have two tables something like this:

President          Term
George Washington  1789-1797
Adams, John        1797-1801
Thomas Jefferson   1801-1809
James Madison      1809-1817
Monroe, James      1817-1825
John Quincy Adams  1825-1829

Open in new window

LastName    FirstName    Party
Washington  George       Independent
Adams       John         Federalist
Jefferson   Thomas       Democratic-Republican
Madison     James        Democratic-Republican
Monroe      James        Democratic-Republican
Adams       John Quincy  Democratic-Republican

Open in new window

What I'm looking for is a query that will return records from Table_1 where there's a match (by FirstName and LastName) in Table_2.

The hitch in this here giddy up is that the names in Table_1 might be First-Last or Last-First (and, in the real data, there might be a salutation of Mr., Ms, or Dr.)

Clifton BardwellProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulEE Topic AdvisorCommented:
Are the titles ONLY Mr., Ms, or Dr. ?
You will need to sanitise the data and extract the names.
I have used this query to do so:
select n.LastName
from Table1
cross apply
(select LTrim(RTrim(REPLACE(REPLACE(REPLACE(' ' + Table1.president + ' ', ' Mr ', ''), ' Dr ', ''), ' Ms ', ''))) as fullname) as r
cross apply
(select charindex(',', r.fullname) as FirstComma, 
 case WHEN charindex(' ', r.fullname) > 0 
      THEN len(r.fullname) - charindex(' ', reverse(r.fullname))+ 1
      ELSE 0
 end as LastSpace) as j
 cross apply
          when j.FirstComma > 0 
          then substring(r.fullname, 1, j.FirstComma-1)
          when j.LastSpace > 0 
          then substring(r.fullname, j.LastSpace+1, len(r.fullname))
          else r.fullname
        end)) as LastName
          when j.FirstComma > 0 
          then substring(r.fullname, j.FirstComma+1, len(r.fullname))
          when j.LastSpace > 0 
          then substring(r.fullname, 1, j.LastSpace)
          else '' 
        end)) as FirstName) as n
LEFT JOIN Table2 ON n.FirstName = Table2.FirstName and n.LastName = Table2.LastName

Open in new window

Here is the SQL Fiddle

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Clifton BardwellProgrammerAuthor Commented:
That seems to work.  Thanks.  :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.