0

I have a set of values (IDs) and i want to use "not in" in my query

if i use

select ...
where ColumnA not in (1234, ...,...,...,..more than 3000 values -IDs)

it works but... but returns resultset of row 2.

I have also a second Column B and want to look the query also in this Column

ColumnA  ColumnB

1234     NULL
NULL     1234

I want that the "not in" looks in both columns but i believe the NULL value make my query not working

I want the result to say ...no row returned

the OR clause doesn't work:

select ...
where ColumnA not in (1234) or ColumnB not in (1234)

gives back row1 and 2.

9
  • Did you try EXISTS? msdn.microsoft.com/en-us/library/ms188336.aspx Commented Feb 18, 2015 at 18:44
  • should not work... how should the query look like? Commented Feb 18, 2015 at 18:59
  • 1
    I think if you change "OR" to "AND" you will get the desired results Commented Feb 18, 2015 at 19:16
  • Use ColumnA is not null and ColumnA not in (). Commented Feb 18, 2015 at 19:18
  • sorry. yes... hard day;-) Commented Feb 18, 2015 at 19:25

3 Answers 3

1

Try something like this:

select ColumnA, ColumnB
from SomeDatabase
where ISNULL(ColumnA, 0) not in (1234)
and ISNULL(ColumnB, 0) not in (1234);
Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest:

select t.*
from table t
where 1234 not in (coalesce(columnA, -1), coalesce(columnB, -1));

This allows you to include the constant value only once in the query.

1 Comment

sorry i not only have to look for '1234' i have maybe 3000 IDs
1
declare @ids table
{
    id int
}

insert into @ids (id)
values
(1),
(2),
(3),
(4),
...

select *
from table
where
    ColumnA is not null and not in (select id from @ids)
and ColumnB is not null and not in (select id from @ids)

12 Comments

i would prefer something without needing permission to write into database
and just put my 'IDs' somewhere between this ()
@sT3v3n: it isn't writing to teh db, it's a variable of type table.
@sT3v3n: it may and probably will cause 2 reads of the same data. Until you cache it or join.
sorry for my non pro sql behavior! so i should write my IDs inside...{ id int } ???
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.