Solved

Using Like

Posted on 2009-05-13
9
172 Views
Last Modified: 2012-05-06
I'm trying to use "Like" like below but this only looks for exact matches. How can I do this?

Note that F_Alert table has no keys related to the other table.
select * from f_alert where 

   criteria like (select Addressee from LAddress lc

                      inner join customeraddress mca on mca.addressid=lc.id  where mca.customerid = '123')

Open in new window

0
Comment
Question by:Camillia
9 Comments
 
LVL 10

Expert Comment

by:Espavo
ID: 24376717
Try this:
select * from f_alert where 

   criteria like '%(select Addressee from LAddress lc

                      inner join customeraddress mca on mca.addressid=lc.id  where mca.customerid = '123')%'

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24376720

select *
from f_alert f
where exists
(select null
from LAddress lc
inner join customeraddress mca on mca.addressid=lc.id
where mca.customerid = '123' AND f.criteria like '%'+Addressee+'%')
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24377358
Try the following two options.  Using LIKE is bad enough without compounding things with an EXISTS. ;-)

Option 1:
 

SELECT F.* 

FROM f_alert F

INNER JOIN LAddress A

ON F.criteria = A.Addressee

INNER JOIN  customeraddress C

ON C.addressid=A.id  

AND C.customerid LIKE '%' + '123' + '%';
 

Option 2:

SELECT F.* 

FROM f_alert F

INNER JOIN LAddress A

ON F.criteria = A.Addressee

INNER JOIN  customeraddress C

ON C.addressid=A.id  

WHERE C.customerid LIKE '%' + '123' + '%';

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 24377473
let me see. Thanks. Yeah, like is not good.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 22

Expert Comment

by:8080_Diver
ID: 24377550
LIKE is Not A Good THing . . . unless it is the only way to get there from here. ;-)
However, when you use LIKE, you just have to be aware that you have just agreed to have potentially very poor performance and, if you are playing with enough data, time outs. ;-)
Where I work, there is an application that may wind up using up to 5 instances of LIKE '%whatever%' on tables with millions of rows . . . and they keep asking me to provide indexes to solve their performance issues. ;-)
0
 
LVL 7

Author Comment

by:Camillia
ID: 24377648
Fraud_alert table has a row with column =330.  LocAddress has a column=33092

It should bring back that "330" row but it doesnt:

select * 

from fraud_alert f 

where exists 

(select null 

from LocAddress lc 

inner join memcustomeraddress mca on mca.addressid=lc.id 

where mca.customerid = '123' AND 

(f.criteria like '%' + PostalCode + '%' OR f.criteria like '%'+Addressee+'%' OR f.criteria like '%'+ Address1 + '%' )
 

)

Open in new window

0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24378271
You've got your LIKEs cross wired.  (And I still done't like the EXISTS approach but I don't have time to alter that part. ;-)
select * 

from fraud_alert f 

where exists 

(select null 

from LocAddress lc 

inner join memcustomeraddress mca on mca.addressid=lc.id 

where mca.customerid = '123' AND 

(PostalCode like '%' + f.criteria + '%' OR Addressee like '%'+f.criteria+'%' OR Address1 like '%'+ f.criteria + '%' )

 

)

Open in new window

0
 
LVL 22

Accepted Solution

by:
8080_Diver earned 500 total points
ID: 24378322
On second thought, it didn't take too long: ;-)
SELECT f.* 

FROM fraud_alert f 

INNER JOIN 

(

 SELECT PostalCode,

        Addressee,

        Address1 

FROM LocAddress lc 

INNER JOIN memcustomeraddress mca 

   ON mca.addressid=lc.id 

WHERE  mca.customerid = '123'

) Z

ON (Z.PostalCode like '%' + f.criteria + '%' ) OR 

   (Z.Addressee like '%'+f.criteria+'%' ) OR 

   (Address1 like '%'+ f.criteria + '%' );

 

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 24378484
8080: your last one worked. Let me try one more thing and will post back
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

15 Experts available now in Live!

Get 1:1 Help Now