0

I have a problem with column [Transfer-from Code].
There is like 30 different values.
But I need all values for [Transfer-from Code] which are x1, x2 and NULL.

Here is the code:

SELECT 
       ILE.[Item No_]           AS ItemNo
      ,It.[Tariff No_]          AS TariffNo
      ,ILE.[Posting Date]       AS PostingDate
      ,ILE.[Location Code]      AS LocationCode
      ,ILE.[Quantity]           AS Qty
      ,CASE 
       WHEN ILE.[Entry Type] = 0 THEN 'Purchase'    
       WHEN ILE.[Entry Type] = 4 THEN 'Transfer'
       ELSE '' END AS EntryType
      ,Trp.[Transfer-from Code] AS SourceLocation
      -- Try to cast from below all NULL values to be test, so I can use them into IN operator. Still not working
      --,ISNULL(Trp.[Transfer-from Code], 'test') AS SourceLocation

FROM [MDR].[dbo].[nav_XX$Item_Ledger_Entry]             AS ILE 
LEFT JOIN [MDR].[dbo].[nav_XX$Item]                     AS It ON ILE.[Item No_] = It.No_
LEFT JOIN [MDR].[dbo].[nav_XX$Transfer_Receipt_Header]  AS Trp on ILE.[Document No_] = Trp.No_

WHERE ILE.[Entry Type] IN ('0','4')
AND ILE.[Posting Date] > '2019-12-11 00:00:00.000'
AND ILE.[Location Code] IN ('X24','XB16')

--From this part below, everything don't work. I'm getting only the values for X1 and X2.

--AND TRP.[Transfer-from Code] IN ('X1','X2',NULL)

--AND TRP.[Transfer-from Code] IN ('X1','X2','test')

/*
AND TRP.[Transfer-from Code] = 'X1'
OR TRP.[Transfer-from Code] = 'X2'
--OR TRP.[Transfer-from Code]= NULL
OR TRP.[Transfer-from Code]= 'test'
*/

Can someone tell me where is my mistake?

5
  • 1
    field IN (...) OR field IS NULL Commented Dec 13, 2019 at 13:14
  • @DmitryBychenko AND TRP.[Transfer-from Code] IN ('X1','X2', IS NULL) it not working. I receive error:Incorrect syntax near the keyword 'IS'. Commented Dec 13, 2019 at 13:18
  • where TRP.[Transfer-from Code] IN ('X1','X2') is not false will return x1, x2 and null. Commented Dec 13, 2019 at 13:31
  • BTW, which dbms are you using? (Perhaps MS SQL Server?) Commented Dec 13, 2019 at 13:33
  • @jarlh MS SQL Server Commented Dec 16, 2019 at 6:57

3 Answers 3

2

Your condition should be:

AND (TRP.[Transfer-from Code] IN ('X1','X2') OR TRP.[Transfer-from Code] IS NULL)

Check for NULLs only with the operators IS and IS NOT.
Never with = or IN because the result of a comparison to NULL is always NULL.

Sign up to request clarification or add additional context in comments.

Comments

1

Replace "=" with "IS", add "()" and it will work.

Try :

AND 
(
    TRP.[Transfer-from Code] = 'X1'
    OR TRP.[Transfer-from Code] = 'X2'
    OR TRP.[Transfer-from Code] IS NULL
)

Comments

1

Null has a special meaning it stands for unknown, doesn't matter etc. That's why it has special syntax to check:

 field IS NULL

In your case

 (field IN (item1, item2, item3) OR field IS NULL)

E.g.

 (TRP.[Transfer-from Code] IN ('X1', 'X2') OR TRP.[Transfer-from Code] IS NULL)

please, note that even if text like

 field = NULL

or

 field IN (null)

is syntactically correct, the outcome will be neither true nor false but NULL when field is NULL (Yes, NULL = NULL is not true):

if field with unknown value (Null) equals to some other unknown value (Null)? It's unknown (Null)

1 Comment

Note that field = NULL and field IN (null) are not valid ANSI SQL syntax. (But accepted by many dbms products.)

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.