Reputation: 656
Hi my knowledge of SQL is not brilliant so was hoping someone can help with this problem.
I have two lists of data that I have imported into SQL Server. I want to compare the two lists and find out how many are in both etc
Usually I would do a simple join but the only column in common with the two lists is a name so I need to combine it with a like command because in one list there are lots of extra characters stopping a normal join from working.
Can any one help give me the correct SQL syntax to get this to work.
Thanks
Upvotes: 2
Views: 31578
Reputation: 50970
Not enough information. You need to tell us:
How many columns you need to join (or approximately join) to compare the lists.
In what way, exactly, the columns need to be manipulated to be considered equivalent.
In general you have two approaches:
Build a comparison with LIKE to compare the columns that are not exactly the same.
Operate on the column(s) with "extra characters" using UPDATE until all the discrepant characters are removed, then apply a regular "=" JOIN.
Upvotes: 0
Reputation: 5474
Assuming you have 2 columns in the 2 tables...
You can check for not equal by changing the where clause (like "and a.column1 <> b.column1")
select count(*)
from tableA a, tableB b
where a.column1 = b.column1
and a.column2 = b.column2
Upvotes: 0
Reputation: 453298
You can use LIKE
in a JOIN
. It would be clearer if you provided some example data but it sounds like you want something like this.
SELECT *
FROM T1
JOIN T2 ON T1.name LIKE T2.name + '%'
Upvotes: 6