Skip to content
Advertisement

Relational Operators Issue With Null

I am having issues with the below select statement not picking up the data when I insert a relational operator.

SELECT C.FIRST_NAME, C.LAST_NAME, B.COMPANY,A.ITEMNO, A.REV, A.DESCRIP
FROM  ARINVT A
INNER JOIN ARCUSTO B 
  ON A.ARCUSTO_ID = B.ID
INNER JOIN PR_EMP C 
  ON B.AR_PR_EMP_ID = C.ID
WHERE A.ARCUSTO_ID = B.ID AND
  B.AR_PR_EMP_ID = C.ID AND
  A.ONHAND <= 0 AND
  A.CUSER7 = 'Y' AND
  A.PK_HIDE <> 'Y'

If I remove the last line of the statement (A.PK_HIDE <> ‘Y’) it picks up the data I am expecting to see. The PK_HIDE column has only 3 different values in the column, ‘Y’, ‘N’ or Null. When removing the last line I pick up the data set where PK_HIDE = ‘N’.

I have also tried this statement:

SELECT C.FIRST_NAME, C.LAST_NAME, B.COMPANY,A.ITEMNO, A.REV, A.DESCRIP
FROM  ARINVT A
INNER JOIN ARCUSTO B 
  ON A.ARCUSTO_ID = B.ID
INNER JOIN PR_EMP C 
  ON B.AR_PR_EMP_ID = C.ID
WHERE A.ARCUSTO_ID = B.ID AND
  B.AR_PR_EMP_ID = C.ID AND
  A.ONHAND <= 0 AND
  A.CUSER7 = 'Y' AND
  A.PK_HIDE = 'N' OR A.PK_HIDE IS NULL

That statement seems to ignore (A.CUSER7 = 'Y') as it pulls all data sets with A.PK_HIDE = 'N' OR A.PK_HIDE IS NULL.

Any help is appreciated. Thank you in advance.

Advertisement

Answer

If you want manage correctly null value in the first query you should use a NVL for PK_HIDE

SELECT C.FIRST_NAME, C.LAST_NAME, B.COMPANY,A.ITEMNO, A.REV, A.DESCRIP
FROM  ARINVT A
INNER JOIN ARCUSTO B 
  ON A.ARCUSTO_ID = B.ID
INNER JOIN PR_EMP C 
  ON B.AR_PR_EMP_ID = C.ID
WHERE A.ARCUSTO_ID = B.ID 
AND B.AR_PR_EMP_ID = C.ID 
AND A.ONHAND <= 0 
AND A.CUSER7 = 'Y' 
AND NVL(A.PK_HIDE, 'N') <> 'Y'

In the second you must wrap the condition for PK_HIDE in ( )

SELECT C.FIRST_NAME, C.LAST_NAME, B.COMPANY,A.ITEMNO, A.REV, A.DESCRIP
FROM  ARINVT A
INNER JOIN ARCUSTO B 
  ON A.ARCUSTO_ID = B.ID
INNER JOIN PR_EMP C 
  ON B.AR_PR_EMP_ID = C.ID
WHERE A.ARCUSTO_ID = B.ID 
AND B.AR_PR_EMP_ID = C.ID 
AND A.ONHAND <= 0 
AND A.CUSER7 = 'Y' 
AND ( A.PK_HIDE = 'N' OR A.PK_HIDE IS NULL) 

if you dont’use ( A.PK_HIDE = 'N' OR A.PK_HIDE IS NULL) your where condition is equivalent to

WHERE ( A.ARCUSTO_ID = B.ID 
AND B.AR_PR_EMP_ID = C.ID 
AND A.ONHAND <= 0 
AND A.CUSER7 = 'Y' 
AND A.PK_HIDE =  ) 
OR A.PK_HIDE IS NULL

that obviuosly produce the wrong result

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement