Solved

Find a matching string

Posted on 2011-03-25
7
598 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Webservices in T-SQL 3 30
SqlServer no dupes 25 34
SQL view 2 25
ms sql + get number in list out of total 7 26
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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

815 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

12 Experts available now in Live!

Get 1:1 Help Now