Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

LIKE operator with INNER JOIN Expand / Collapse
Author
Message
Posted Thursday, November 01, 2012 9:20 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:20 AM
Points: 35, Visits: 114
Hi all,

I have to join two tables with the common field being the [SONumber]

Well the problem I am facing is SONumber in table[Table1] which I am joining contains the value "123" and the value on the other table[Table2] is "123/1", "123/2"
I want to return all the values where SONumber is like '123%'

I tried
SELECT Table1.Col1, Table1.Col2, Table1.Col3, Table2.Col1 FROM Table2 
LEFT OUTER JOIN Table1 ON Table1.SONumber like '%' + Table2.SONumber + '%' WHERE ...

But its not giving me desired results.

Any help will be greatly appreciated.

Regards,
Deepak
Post #1379880
Posted Thursday, November 01, 2012 9:25 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Today @ 5:33 AM
Points: 1,498, Visits: 18,143
Try swapping Table1.SONumber and Table2.SONumber

Table2.SONumber  like '%' + Table1.SONumber  + '%'



____________________________________________________

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537

Never approach a goat from the front, a horse from the rear, or a fool from any direction.
Post #1379882
Posted Thursday, November 01, 2012 9:27 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:20 AM
Points: 35, Visits: 114
Hi Mark,

Thanks for your reply. I have already tried that with no luck...

Cheers,
Deepak
Post #1379886
Posted Thursday, November 01, 2012 10:07 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: 2 days ago @ 8:46 AM
Points: 8,547, Visits: 8,204
This is a bit unorthodox but I think it would work in your case. Given that you can't expect great performance out of this due to the strange way you need to find your data I think this will work.

create table #Table1
(
SONumber varchar(10)
)

create table #Table2
(
SONumber varchar(10)
)

insert #Table1
select '123'

insert #Table2
select '123/1'

select *
from #Table1 t1
left join #Table2 t2 on CHARINDEX(t1.SONumber, t2.SONumber) > 0

drop table #Table1
drop table #Table2



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Post #1379916
Posted Thursday, November 01, 2012 2:59 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, May 16, 2013 1:32 PM
Points: 294, Visits: 1,101
Assuming that your data is always in the format that you have indicated, you could try
create table #Table1
(
SONumber varchar(10)
)

create table #Table2
(
SONumber varchar(10)
)

insert #Table1
select '123'

insert #Table2
select '123/1'

select *
from #Table1 t1
left join #Table2 t2 on t1.SONumber = SUBSTRING(t2.SONumber, 1, len(t1.SONumber))

drop table #Table1
drop table #Table2

Sticking with the like option this should work as well

select * 
from #Table1 t1
left join #Table2 t2 on t2.SONumber like t1.SONumber + '%'

Post #1380044
Posted Thursday, November 01, 2012 3:23 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 3:26 PM
Points: 958, Visits: 1,917
Micky,
Your first query is not sargable and that would make it pretty inefficient compared to the second one.



Luis C.
Please don't trust me, test the solutions I give you before using them.
Forum Etiquette: How to post data/code on a forum to get the best help
Post #1380059
Posted Thursday, November 01, 2012 3:31 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, May 16, 2013 1:32 PM
Points: 294, Visits: 1,101
Luis Cazares (11/1/2012)
Micky,
Your first query is not sargable and that would make it pretty inefficient compared to the second one.

True
Not knowing what the data is for, I suspect a better option would be to split the SONumber in table 2 to SONumber and Suffix, then there would be no reason for the Like join
Post #1380063
Posted Thursday, November 01, 2012 3:50 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 3:26 PM
Points: 958, Visits: 1,917
mickyT (11/1/2012)
Luis Cazares (11/1/2012)
Micky,
Your first query is not sargable and that would make it pretty inefficient compared to the second one.

True
Not knowing what the data is for, I suspect a better option would be to split the SONumber in table 2 to SONumber and Suffix, then there would be no reason for the Like join

True as well.
Most problems in T-SQL can be prevented or corrected with a good DB design.



Luis C.
Please don't trust me, test the solutions I give you before using them.
Forum Etiquette: How to post data/code on a forum to get the best help
Post #1380068
Posted Thursday, November 01, 2012 7:18 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945, Visits: 2,782
>> I have to join two tables with the common field [sic] being the “so_nbr” <<

You don't know the differences between fields and columns; you failed to use ANSI/ISO Standard double quotes instead of the MS square brackets; you do not know ISO-11179 naming rules; you did not post any DDL. This is not a good start.

>> Well the problem I am facing is so_nbr in table, “Table1” which I am joining contains the value "123" [sic: column name, not value] and the value on the other table, “Table2” is "123/1", "123/2" <<

SQL uses double quotes for data element names and single quotes for strings. So this is garbage! I think that if you spoke SQL, you would have used single quotes. Now look at the order of your outer join and which table you preserved and the LIKE pattern you used. Try this

SELECT Table1.col1, Table1.col2, Table1.col3, Table2.col1
FROM Table2
LEFT OUTER JOIN
Table1
ON Table2.so_nbr
LIKE Table1.so_nbr + '/[12]'
WHERE …;

The '%' wildcard should be avoided if possible. It is slow and can give you unwanted data. Use underscores for fixed length matches and the simple regular expression instead.



Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1380101
Posted Tuesday, November 06, 2012 3:12 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:11 AM
Points: 1,042, Visits: 1,438
This would also work fine for your requirement:

Create Table Ex 
(Id Int Identity(1,1),
Value NVarchar(5) )

Create Table Ex1
(Id Int Identity(1,1),
Value NVarchar(5) )

Insert Into Ex
Select '123'

Insert Into Ex1
Select '123/1'

Select * From Ex As a JOIN Ex1 As b ON b.Value LIKE Left(a.Value, CHARINDEX('/', a.Value)) + '%'



Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden
Post #1381467
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse