Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to NOT match on an exception list


How to NOT match on an exception list

Author
Message
SQLWannabe
SQLWannabe
SSC-Enthusiastic
SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)SSC-Enthusiastic (118 reputation)

Group: General Forum Members
Points: 118 Visits: 571
If I have a table of Products that have a ProductName and I have a table of ProductNames that I don't want the first table matched to, what is the best process in which to do this? The names will be LIKE each other, but will not match exactly. When I pull data, I only want the ones from dbo.Products where they don't look like the Products from dbo.Exceptions. In my example, I want to pull back Products that don't look like WidgetD or WidgetE.

Normally I would write:
SELECT ProductDesc FROM dbo.Products WHERE ProductDesc NOT LIKE '%WidgetD%' AND ProductDesc NOT LIKE '%WidgetE%'

The query should return WidgetA, WidgetB, and WidgetC (3 records)

However, I don't want to jack with the code every time a new exception is added.

Is there a better method? (example code below)

CREATE TABLE [dbo].[Products](
[ProductDesc] [varchar](200) NULL
)


INSERT INTO Products(ProductDesc) SELECT 'WidgetA'
INSERT INTO Products(ProductDesc) SELECT 'WidgetB'
INSERT INTO Products(ProductDesc) SELECT 'WidgetC'
INSERT INTO Products(ProductDesc) SELECT 'WidgetD'
INSERT INTO Products(ProductDesc) SELECT 'WidgetE'

CREATE TABLE [dbo].[Exceptions](
[ProductDesc] [varchar](200) NULL
)

INSERT INTO Exceptions(ProductDesc) SELECT 'Big WidgetD'
INSERT INTO Exceptions(ProductDesc) SELECT 'Small WidgetE'
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14942 Visits: 38940
fantastic job providing the DDL and sample data!

I think in this case, you need to match partial strings, right?

you will want to join the tables together, but use a LIKE statement for the join criteria,(where you might be used to A.ID = B.ID

this seems to do what i think we are after:


SELECT Products.*
FROM Products
LEFT OUTER JOIN Exceptions
ON Exceptions.ProductDesc LIKE '%' + Products.ProductDesc + '%'
WHERE Exceptions.ProductDesc IS NULL



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24171 Visits: 37936
Something like this:



CREATE TABLE [dbo].[Products](
[ProductDesc] [varchar](200) NULL
)


INSERT INTO Products(ProductDesc) SELECT 'WidgetA'
INSERT INTO Products(ProductDesc) SELECT 'WidgetB'
INSERT INTO Products(ProductDesc) SELECT 'WidgetC'
INSERT INTO Products(ProductDesc) SELECT 'WidgetD'
INSERT INTO Products(ProductDesc) SELECT 'WidgetE'

CREATE TABLE [dbo].[Exceptions](
[ProductDesc] [varchar](200) NULL
)

INSERT INTO Exceptions(ProductDesc) SELECT 'Big WidgetD'
INSERT INTO Exceptions(ProductDesc) SELECT 'Small WidgetE'

GO

select
*
from
[dbo].[Products] p
where
not exists(select 1 from [dbo].[Exceptions] e where e.ProductDesc like '%' + p.ProductDesc + '%');

GO

drop table [dbo].[Products];
drop table [dbo].[Exceptions];
GO




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search