April 3, 2013 at 5:18 am
Hi everyone
I have 2 tables in SQL and i would like to join them together however the fields I wish to join on have slightly different information.
Table1 has the field Region and Table2 has the field Region. However Table2s Region field contains a * either side of the string. So in Table1 it would appear as NORTH whilst in Table2 as *NORTH*.
Is there a way I can join the 2 tables when they differ in this way?
Thanks in advance.
BO
April 3, 2013 at 5:23 am
Two quick options
either
select ..
from a
inner join b on a.col1 = '"' + b.col1 + '"'
or
select ..
from a
inner join b on replace(a.col1,'"','') = b.col1
but either will probably not perform too well. so it depends how often you need to do this.
If possible it would be better to update the data in one table to remove the quotes.
Mike John
April 3, 2013 at 5:38 am
Cheers for the quick update...
Actually they are stars * rather than quotes "" Tried the same logic but it didn't seem to like it - no error but no join either...
Any pointers?
April 3, 2013 at 6:13 am
Sorry for misreading the stars/quotes - eyes must be getting old!
No reason I can think of for one or the other option not working - unless you actually have leading or trailing spaces in either as well as the stars
try something like
select '[' + col + ']' from a to see if you actually have extra characters in there.
Mike John
April 3, 2013 at 6:27 am
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2
ON t2.Region = '*'+t1.Region+'*'
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 3, 2013 at 6:47 am
These solutions are both great guys - thanks so much!!
One other thing, what if the region in Table2 did not only have multiplication (star) signs either side but also other text? Would it be possible to find the region within the text and then link to Table1?
Thanks again!!
BO
April 3, 2013 at 6:50 am
Use the LIKE operator:
SELECT *
FROM Table1 t1
INNER JOIN Table2 t2
ON t2.Region LIKE '*'+t1.Region+'*%'
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 3, 2013 at 7:20 am
Cheers for this. I actually got it the wrong way round though!!
The table with the multiplication signs round the REGION does not have the extra text its the other table...
April 3, 2013 at 8:21 am
Ignore my last message, I'm being an idiot as usual...
April 3, 2013 at 8:22 am
Thanks both for your help!!
BO
April 3, 2013 at 1:23 pm
The following code only joins on the first four records in #TableRegionOther when I want it to bring back everything apart from (' SouthWest ', 19)...
Any clues as to what I might be doing wrong?
Thanks
BO
create table #TableRegion
(
[Region] varchar(10)
)
insert into #TableRegion (Region)
values ('*North*'),
('*East*'),
('*South*'),
('*West*');
create table #TableRegionOther
(
[Region] varchar(25),
[Sales] int
)
insert into #TableRegionOther (Region, Sales)
values ('North', 123),
('East', 43),
('South', 765),
('West', 9364),
('PHC North', 4),
('East TRC', 4356),
(' SouthWest ', 19),
('dd South rts ', 234);
SELECT *
FROM #TableRegion t1
inner JOIN #TableRegionOther t2
ON t1.Region like '*%'+t2.Region+'%*'
April 3, 2013 at 1:42 pm
The queries will be inefficient, unless you correct your data.
Here's one option, but you'll have a problem with "SouthWest"
SELECT *
FROM #TableRegion t1
FULL JOIN #TableRegionOther t2
ON CHARINDEX( SUBSTRING(t1.Region, 2, 4), t2.Region) > 0
And another way:
SELECT *
FROM #TableRegion t1
FULL JOIN #TableRegionOther t2
ON t2.Region LIKE '%' + REPLACE( t1.Region, '*', '') + '%'
April 3, 2013 at 1:57 pm
Cheers Luis
This works great but like you say the data will need a little tweaking...
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply