Solved

Find a matching string

Posted on 2011-03-25
7
619 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

728 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