Solved

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

Posted on 2011-03-21
7
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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 41

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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