Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Match two words in a column

## I have two tables, and both have a column called full_name. I want to match at least two words in the full name column. For example, "John Alder Smith" and "John F Smith" is a match, while "Peter Duncan Doyle" and "Peter Parker" is not a match.

I have two tables, and both have a column called full_name. I want to match at least two words in the full name column. For example, "John Alder Smith" and "John F Smith" is a match, while "Peter Duncan Doyle" and "Peter Parker" is not a match. Thanks for your help.

The SQL part of the solution to this problem is to use a cross join, and then a WHERE clause to do the match:

```select t1.full_name as t1_name
, t2.full_name as t2_name
from table_one as t1
cross
join table_two as t2
where t1.full_name resembles t2.full_name ```

Why a cross join? Just to acknowledge that we are comparing every name in table one to every name in table two in order to find matches. At least, that's what the problem sounded like to me.

Now, about this mysterious resembles operator. Of course, there is no such thing, at least not tailor-made to match at least two words in two columns. Our challenge now is to find a way to do this with SQL.

The tough part is breaking up the name into words. For the general case, an unknown number of words, we might employ an auxiliary integers table, but let's assume that for full names, four words (names) is a good working maximum, as this simplifies the query a bit.

```select t1.full_name as t1_fullname
, t2.full_name as t2_fullname
from table_one as t1
cross
join table_two as t2
where case when ' '||t1.full_name||' '
like '% '||word(t2.full_name,1)||' %'
then 1 else 0 end
+ case when ' '||t1.full_name||' '
like '% '||word(t2.full_name,2)||' %'
then 1 else 0 end
+ case when ' '||t1.full_name||' '
like '% '||word(t2.full_name,3)||' %'
then 1 else 0 end
+ case when ' '||t1.full_name||' '
like '% '||word(t2.full_name,4)||' %'
then 1 else 0 end
>= 2```

Each CASE expression compares a separate word from t2.full_name, to the complete t1.full_name. Each LIKE comparison consists of two terms, with a space appended to both the front and back of each term being compared, like this:

`   ' Peter Duncan Doyle ' like '% Peter %'`

We need spaces around the name Peter in the right term, '% Peter %', because we don't want to match Peterson. We therefore also need to append a space to both the front and back of the left term, the entire full name, in order to find a word at the beginning or end of the full name.

Thus we test the first four words of t2.full_name, and for every word found within t1.full_name, we add 1 to a total. And if this total is 2 or more, the full names are considered to match.

The only thing we haven't done yet is explain how to extract the separate words out of t2.full_name. As you probably guessed, there is no WORD function. Depending on your database system, you might use some combination of nested POSITION and SUBSTRING functions, to extract words based on how many spaces you detect in the full name going from left to right. Granted, by the time you get to the CASE expression for the fourth word, with POSITION and SUBSTRING functions nested four deep, it does get ugly. For this reason, look to see if your database system offers any other string handling functions to make this part easier. For example, MySQL has the SUBSTRING_INDEX function, which can make this task easier. But if your database system allows you to declare a user defined function, then you could write your own WORD function and use it exactly as shown above.

This was last published in October 2005

## Content

Find more PRO+ content and other member only offers, here.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Blockchain data disruption aborning, IDC analyst says

Blockchain data technology disruption may be in the offing. IDC's Stewart Bond says architecture at the core of controversial ...

• ### Potent NoSQL architecture engaged for building new applications

Behind hyperbolic terms like 'digital transformation' are innovative systems of engagement. DataStax CEO Billy Bosworth joins ...

• ### Finance data governance program gets new start, broader focus

Businesses constantly need to evolve their programs for governing data. Nationwide's finance data governance team shares how it ...

Most companies have data monetization opportunities they could exploit, experts say. But a clear strategy and long-term plan are ...

• ### Self-service BI and analytics tools face evolving set of challenges

Self-service BI tools have become the go-to standard for business intelligence software, but users are pushing vendors to rethink...

• ### Data science and analytics methods must balance innovation, value

In this episode of the Talking Data podcast, find out how data science teams at Cisco and LinkedIn navigate the process of doing ...

## SearchSAP

• ### SAP CEO Bill McDermott touts the intelligent enterprise

In a roundtable at SAP Ariba Live 2017, SAP CEO Bill McDermott and SAP Ariba President Alex Atzberger discuss the intelligent ...

• ### Future of Ariba Network on display at SAP Ariba Live

SAP Ariba Live shows enhancements to procurement software and looks at the future of the SAP Ariba Network, including machine ...

SAP Vora has been updated to include features that make it easier to deploy and use to get insights from Hadoop big data; SAP IBP...

## SearchSQLServer

• ### Use these commands to deploy SQL Server Docker containers

Containerized apps are everywhere, but you can also run databases in containers. Follow these commands to customize SQL Server ...

• ### Redgate tools help bring database DevOps to Visual Studio 2017

So far, data has been left out of the DevOps discussion. But DevOps for databases could grow if tools like Redgate's bundle ...

• ### Conquer real-time operational analytics in SQL Server 2016

Analytics helps improve business operations, and SQL Server 2016 users can master it -- in real time, using operational data in a...

## TheServerSide

• ### Does reading XML while writing JSON make me a bad person?

I'm always writing JSON code, loving its flexibility and forgiveness. But when I call an external service, I want to be reading ...

• ### Stop adding web UI frameworks like JSR-371 to the Java EE spec

Java EE's Servlet and JSP API provide a fine foundation for handling a web-based request-response cycle. Adding UI frameworks ...

• ### Declarative Jenkins pipeline the latest new feature in DevOps tooling

Looking to get more out of your DevOps tooling? The declarative Jenkins pipeline helps take continuous integration and continuous...

## SearchDataCenter

• ### IoT certifications land on data center admins' to-do list

IoT is still an emerging field, but data center admins should at least consider certification options to make their resume stand ...

• ### Manage and optimize IBM z Systems software costs

Some mainframe users find it tough to navigate IBM's sub-capacity pricing model. Consider tools like SCRT to more efficiently ...

• ### How hyper-converged systems change IT management

Implementing a hyper-converged infrastructure product is a big decision and one that needs to include a discussion about how ...

## SearchContentManagement

• ### How SharePoint paved the way for successful Office 365 services

SharePoint has developed many valuable enhancements through the years. However, several of those features have exceled as ...

• ### How to sort through redundancies between SharePoint and Office 365

Trying to decide between SharePoint Online and Office 365, but confused by the redundant features? Expert Scott Robinson offers ...

• ### Protecting content from the enterprise collaboration black hole

As the need for enterprise collaboration grows, so does the need for security. Organizations need to understand when confidential...

## SearchFinancialApplications

• ### Corporate wellness software can help employers reduce turnover

Data from a corporate wellness technology vendor shows that engaged employees and ones who use wellness programs are more likely ...

• ### HR analytics software boosts employee retention, lifts bottom line

With data analytics tools, HR can measure aspects such as employee engagement and turnover of staff and use the results to become...

• ### Learning management platform helps keep Unisys employees up to date

The vast majority of employees at Unisys said they were more effective at their jobs after completing training on the cloud-based...

Close