Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Non-Matches with a SQL IN

Author  Topic 

ddouma
Starting Member

10 Posts

Posted - 2007-11-08 : 10:32:05
Probably an easy one, but the solution escapes me. I want to show all records that aren't found in the IN statement. So using the example table and SQL statement below, I would like to get back 'Mark' and 'Ted' as non-matching items in my IN statement.

FirstName
---------
Doug
Fred
Bob

SELECT FirstName from TABLE1
WHERE FirstName IN (
'Doug',
'Fred',
'Bob',
'Mark',
'Ted',
)

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-11-08 : 10:35:39
NOT IN ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ddouma
Starting Member

10 Posts

Posted - 2007-11-08 : 10:50:57
Won't that return all records that don't have Doug, Fred, Bob, Mark and Ted as the FirstName?

I want to know that Mark and Ted where not found in my IN statement
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-11-08 : 11:04:21
The NOT IN statement is executed PER RECORD, not the entire column...



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-11-08 : 11:11:08
I think i got what you mean
try this
SELECT n.FirstName
FROM (
SELECT 'Doug' AS FirstName UNION ALL
SELECT 'Fred' AS FirstName UNION ALL
SELECT 'Bob' AS FirstName UNION ALL
SELECT 'Mark' AS FirstName UNION ALL
SELECT 'Ted' AS FirstName
) n
LEFT JOIN TABLE1 t
ON n.FirstName = t.FristName
WHERE t.FristName IS NULL



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

ddouma
Starting Member

10 Posts

Posted - 2007-11-08 : 11:51:24
That's exactly what I needed! Thank you very much. DD.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-11-08 : 13:46:20
Another satisfied customer!

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -