Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Find a matching string

Posted on 2011-03-25
7
Medium Priority
?
625 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 400 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 400 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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 1200 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

610 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