Solved

Find a matching string

Posted on 2011-03-25
7
612 Views
Last Modified: 2012-05-11
I need a SQL to find a matching string between two table joins

TableA
----------

12345
54321
98989
98080

TableB
---------
+112345
+254321
+398989
+498080
+809009
+687698

Results
----------

+112345
+254321
+398989
+498080

How can I do that?

Thanks,

~Sve
0
Comment
Question by:sventhan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 35216076
SELECT FieldB FROM TableA INNER JOIN TableB ON FieldB LIKE '%' + FieldA + '%'
0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 100 total points
ID: 35216078

Join the tables or use IN keyword

select field1 from table1
inner join table2 on table2.field1 = table1.field1

or

select field1 from table1
where field1 in 9select field1 from table2)
0
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 100 total points
ID: 35216125
Select * from TableB TB where TB.Col1 like '%'+(select TA.Col1 from TableA TA)

or to specify the preceding number of characters:

Select * from TableB TB where TB.Col1 like '__'+(select TA.Col1 from TableA TA)
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 18

Author Comment

by:sventhan
ID: 35216871
@ ewangoya

There is no matching rows between the tables.


@Cluskitt

Yours running for long time. I've 40 million rows in the table

@ jimyX

Getting an Error saying that subquery returns more than 1 row. The Data I've posted is just a sample

Thanks to you all.
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 300 total points
ID: 35216920
You'll have that many if you have many similar fields. For example, if TableA has the value '12345' and TableB has the values: '+112345', '+212345', '-112345', it will create 3 rows.
Also, if you have 100 values in TableB with the same field, it will create 100 rows with the same value. Also, keep in mind that joining on String fields that aren't indexed is very slow.

If all you want to match are the last 5, then you'd be better off with:

SELECT FieldB FROM TableA INNER JOIN TableB ON FieldA = RIGHT(FieldB, 5)

If you want to remove duplicates_

SELECT FieldB FROM TableA INNER JOIN (SELECT DISTINCT FieldB FROM TableB) TableB ON FieldA = RIGHT(FieldB, 5)
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35217010

Yes, my query will not work since its looking for an exact march
I missed a point in your question
0
 
LVL 18

Author Closing Comment

by:sventhan
ID: 35217059
Thanks experts. I've get the idea.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question