SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to solve this in inner join ?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

247 Posts

Posted - 12/09/2012 :  00:46:43  Show Profile  Reply with Quote
Hi there,

I have a select statement using inner join, but joined tables do not have consistent data. See following:

table1 has column of Code
Code | ...
ABC NXY |
ABC |
ABC NXY |
...

table2 has column of Model
Model |
ABC
CBC
DDB
......

Now I have select statement:

select * from table1 t1
inner join table2 t2 on t2.Model = t1.Code
where ....


Actually I need all records which have ABC in Code column. Can the aboe statement rule out all the records which contain 'ABC'? Seems like you can't use LIKE in inner join statement. How can I include all the records which contains 'ABC' in table1, no matter it's 'ABC NXY' or 'ABC NXY' or ... Thanks in advance.

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/09/2012 :  01:53:24  Show Profile  Reply with Quote
select t1.* from table1 t1
inner join table2 t2 on t1.Code like '%' +t2.Model+ '%'
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/09/2012 :  10:12:10  Show Profile  Reply with Quote
If it is consistent starting with ABC

select * from table1 t1
inner join @t2 table2 on t2.Model = Left(t1.Code,3)
Go to Top of Page

allan8964
Posting Yak Master

247 Posts

Posted - 12/09/2012 :  10:13:39  Show Profile  Reply with Quote
thanks sodeep. i tried this, no error but returns either.
Go to Top of Page

allan8964
Posting Yak Master

247 Posts

Posted - 12/09/2012 :  10:46:57  Show Profile  Reply with Quote
well, in that case i don't need join table2. I can use

where code like '%ABC%'

But I need get ModelId from table2. That's why I use join. The issue here is: does like work in join statement?
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/09/2012 :  11:05:42  Show Profile  Reply with Quote
Yes. Did you try mine?
Go to Top of Page

allan8964
Posting Yak Master

247 Posts

Posted - 12/09/2012 :  11:10:25  Show Profile  Reply with Quote
did I tell you it did not work?
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/09/2012 :  11:13:46  Show Profile  Reply with Quote
What does this give you?

select t1.*,t2.ModelId from table1 t1
inner join table2 t2 on t1.Code like '%' +t2.Model+ '%'


If you question is different, post with clear example and expected so we can help
Go to Top of Page

allan8964
Posting Yak Master

247 Posts

Posted - 12/09/2012 :  11:14:04  Show Profile  Reply with Quote
I am sorry, sodeep. Actually I typed it wrong in the first post. Table1 Code of data look like this:

ABC NXY
ABC
TYG ABC
GTR ABC UYT

Situation is ABC may in any of te three positions. That's the hard part. If just in left, that's easy. Sorry for typo.
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/09/2012 :  11:21:21  Show Profile  Reply with Quote
Declare @T Table(Code Varchar(20))

Insert into @T
Select 'ABC NXY' union all
Select 'ABC' union all
Select 'TYG ABC' union all
Select 'GTR ABC UYT'

Declare @S Table(ModelId Varchar(10))

Insert into @S
Select 'ABC' union all
Select 'CBC' union all
Select 'DDB'

select t1.*,t2.ModelId from @T t1
inner join @S t2 on t1.Code like '%' + t2.ModelId+ '%'

Code	ModelId
ABC NXY	ABC
ABC	ABC
TYG ABC	ABC
GTR ABC UYT	ABC

Edited by - sodeep on 12/09/2012 11:22:41
Go to Top of Page

allan8964
Posting Yak Master

247 Posts

Posted - 12/09/2012 :  11:43:17  Show Profile  Reply with Quote
great! sodeep, It's perfect. I figured out why it did not work with my codes ... some other joins affected it ... now fixed. Thank you so much!
Go to Top of Page

sodeep
Flowing Fount of Yak Knowledge

USA
7174 Posts

Posted - 12/09/2012 :  14:07:55  Show Profile  Reply with Quote
welcome
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000