Searching for Records With First And Last Names

I have two tables something like this:

Table_1
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


Table_2
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.)

TIA
Clifton BardwellProgrammerAsked:
Who is Participating?
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.

PortletPaulfreelancerCommented:
Are the titles ONLY Mr., Ms, or Dr. ?
0
chaauCommented:
You will need to sanitise the data and extract the names.
I have used this query to do so:
select n.LastName
       ,n.FirstName
       ,Table1.Term
       ,Table2.Party
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
 (SELECT
        LTrim(RTrim(
        case 
          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
       ,LTrim(RTrim(
        case 
          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
0

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.  :-)
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.