Solved

sql where  not like from other table

Posted on 2013-05-24
8
430 Views
Last Modified: 2013-05-26
I have 2 tables. I table with alot of data/columns and 1 table with 1 column exclusions.
i need a select statement that will show tableA where columnA is NOT LIKE '%tableB.text%"
AND tableA.columnB is NOT LIKE '%tableB.text%'

The closest I got to work is this which works as an exact match. How can I adjust this to show like with %% on both sides
select * from errorlogs
where errorlogs.filename not in
(select exclusion.text from exclusion)
and errorlogs.ip not in
(select exclusion.text from exclusion)

Open in new window

0
Comment
Question by:rivkamak
  • 3
  • 3
  • 2
8 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39195006
There may be a better way to do this with a function and CROSS APPLY but...

Also I'm assuming you have a unique identifier of some kind in errorlogs which I am referring to as ErrorLogID

SELECT errorlogs.*
FROM errorlogs
WHERE ErrorLogID NOT IN (
SELECT DISTINCT errorlogs.ErrorLogID
FROM errorlogs
CROSS JOIN exclusion
WHERE errorlogs .filename LIKE '%' + exclusion.text + '%')
0
 

Author Comment

by:rivkamak
ID: 39195018
That didn't work. I didn't get anything back
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39195027
Please post your table creation scripts for errorlogs and exclusions so I can play with it.
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 69

Expert Comment

by:Scott Pletcher
ID: 39195270
SELECT el.*
FROM dbo.errorlogs el
INNER JOIN dbo.exclusion ex ON
    el.ColumnA NOT LIKE '%' + ex.text + '%' AND
    el.columnB NOT LIKE '%' + ex.text + '%'
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39195377
I don't think that will work because you will actually get duplicate errorlog records for each exclusion entry that doesn't match.  As I understand it each errorlog record needs to be compared against eachexclusion entry and only included in the resultset if there is NO match found.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39195467
DOH, now that I see more of the q, you're probably right.  I went by only part of the original q.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39195475
SELECT el.*
FROM dbo.errorlogs el
WHERE
    NOT EXISTS(
        SELECT 1
        FROM dbo.exclusion ex
        WHERE
            el.filename LIKE '%' + ex.text + '%' OR
            el.ip LIKE '%' + ex.text + '%'
    )
0
 

Author Closing Comment

by:rivkamak
ID: 39198429
Thank you
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

679 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