Solved

Find a matching string

Posted on 2011-03-25
7
580 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
  • 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:ewangoya
ewangoya 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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
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:ewangoya
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now