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 2008 Forums
 Transact-SQL (2008)
 Using not equal to in statement

Author  Topic 

PoseyRobert
Starting Member

27 Posts

Posted - 2015-04-06 : 17:08:50
Hi,

I can not understand why this simple statement is not working correctly.

SELECT *
FROM ITEMS_IPLS_LOCATIONS as iil
WHERE iil.Location_Code <> 'QAPROD' and iil.Facility_Code <> '04'

The statement should only exclude facility 04 with a location 'QAPROD'

My problem is that the statement is excluding all records that contain a facility 04.

example :

04 FG
04 QAPROD
04 WIP


My result should be
04 FG
04 WIP

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2015-04-06 : 17:14:04
For what you are describing, logically what you want is the following:
SELECT  *
FROM ITEMS_IPLS_LOCATIONS AS iil
WHERE NOT( iil.Location_Code = 'QAPROD' AND iil.Facility_Code = '04');
That can also be expanded to be the following:
SELECT  *
FROM ITEMS_IPLS_LOCATIONS AS iil
WHERE iil.Location_Code <> 'QAPROD' OR iil.Facility_Code <> '04'

If you have nulls in any row for Location_Code or Facility_Code, you will need to modify your query slightly.
Go to Top of Page

PoseyRobert
Starting Member

27 Posts

Posted - 2015-04-06 : 17:17:13
Thanks James for the quick response.

That fixed the problem.
Go to Top of Page
   

- Advertisement -