Solved

How do I include NULL in NOT IN clause?  SQL Sever

Posted on 2011-03-21
7
292 Views
Last Modified: 2012-05-11
I'm trying to retrieve Null value along with my specified value.

SubcategoryID2 has 4 values,  'STO', 'ORD', ' ', NULL
I would always like to apply this condition (deleted <>0)
If I mix up AND and OR condition, I get wrong results.

What didn't work
- WHERE (SubcategoryID2 NOT IN ('STO','ORD')) AND (deleted <> 1)
-----results ------ SubcategoryID2 doesn't include NULL

- WHERE (SubcategoryID2 NOT IN ('STO','ORD')) AND (deleted <> 1) OR (SubcategoryID2 IS NULL)
-----results ------ SubcategoryID2 has correct results, but deleted column also includes value of 1

How do I write this condition? What I need to do here is
1, deleted<>1 always applies
2, I would like to include NULL value in results.

here is my current SQL syntax

SELECT        ImageID, Priority, SubcategoryID2
FROM            Items
WHERE        (SubcategoryID2 NOT IN ('STO', 'ORD')) AND (deleted <> 1)
ORDER BY Priority

Thank you
0
Comment
Question by:jtuttle99
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 12

Expert Comment

by:enachemc
ID: 35183313
WHERE (SubcategoryID2 NOT IN ('STO','ORD') and SubcategoryID2 is not null ) AND (deleted <> 1)
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 35183380
another way:

WHERE isNull(SubcategoryID2,'NULL') NOT IN ('STO','ORD','NULL') AND deleted <> 1
0
 

Author Comment

by:jtuttle99
ID: 35183408
sorry, I didn't explain right.
I would like to treat ' ' and NULL same.
So, If I specify NOT IN ('STO', 'ORD'), I would like to get results ' ' AND NULL
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:jtuttle99
ID: 35183424
WHERE        (deleted <> 1) AND (ISNULL(SubcategoryID2, N'NULL') NOT IN ('STO', 'ORD'))

may work....
0
 
LVL 12

Expert Comment

by:enachemc
ID: 35183443
WHERE (SubcategoryID2 NOT IN ('STO','ORD', '') and SubcategoryID2 is not null ) AND (deleted <> 1)
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 35183474
another other way:

WHERE isNull(SubcategoryID2,'') NOT IN ('STO','ORD','') AND deleted <> 1
0
 
LVL 40

Expert Comment

by:Sharath
ID: 35183507
you need this.
 WHERE (SubcategoryID2 NOT IN ('STO','ORD') 
         OR ISNULL(SubcategoryID2,'') = '') 
       AND deleted <> 1

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

13 Experts available now in Live!

Get 1:1 Help Now