SELECT NOT IN (Trouble)

  • Hi All

    Can you please let me know what am I doing wrong here?

    SELECT NOT IN is not returning rows that should be returned.

    Select

    * from [CIATStaging].[dbo].[ResponseSelectionDictionary] where SelectionValue Like 'False%' OR SelectionValue Like 'True%'

    --Returns 2 Rows with SelectionValue 'True' and 'False'

    SELECT

    * FROM CADS.dbo.AssessmentSelection where SelectionValue Like 'False%' OR SelectionValue Like 'True%'

    --Returns 0 Rows

    SELECT

    DISTINCT R.[SelectionValue] RS, A.[SelectionValue] [AS] FROM [CIATStaging].[dbo].[ResponseSelectionDictionary] R LEFT OUTER JOIN CADS.dbo.AssessmentSelection A ON A.[SelectionValue] = R.[SelectionValue]WHERE A.[SelectionValue] IS NULL

    --Returns 2 Rows with 2 NULLs in [AS]

    SELECT

    DISTINCT [SelectionValue] FROM [CIATStaging].[dbo].[ResponseSelectionDictionary] WHERE [SelectionValue] NOT IN (SELECT DISTINCT [SelectionValue] FROM CADS.dbo.AssessmentSelection)

    --Returns 0 rows

    I have already used NOT IN Clause in many places in my code and it might prove disasterous in future. Any Ideas other than changing the code to LEFT OUTER JOIN?

    Thanks

    Gary

     

  • Try this:

    SELECT DISTINCT [SelectionValue] FROM [CIATStaging].[dbo].[ResponseSelectionDictionary] WHERE isnull([SelectionValue],'') NOT IN (SELECT DISTINCT [SelectionValue] FROM CADS.dbo.AssessmentSelection)

  • Hi Sreejith

    Same - 0 rows returned

  • The issue is with NULL values. NULL don't equal anything and they don't not equal anything. NULL is like saying I don't know what this value is. So, when you do the NOT IN, you won't get NULLs returned because SQL Server can't tell if it is or isn't in the subSelect.

    -SQLBill

  • You could try:

    SELECT DISTINCT [SelectionValue] FROM [CIATStaging].[dbo].[ResponseSelectionDictionary] WHERE [SelectionValue] NOT IN (SELECT DISTINCT [SelectionValue] FROM CADS.dbo.AssessmentSelection)

    OR IS NULL

    -SQLBill

  • The reason is that a NULL value is in the subquery. Since NULL is a unknown value, all values from the main query are eliminated. Change to this:

    SELECT DISTINCT [SelectionValue] FROM [CIATStaging].[dbo].[ResponseSelectionDictionary] WHERE [SelectionValue] NOT IN (SELECT DISTINCT [SelectionValue] FROM CADS.dbo.AssessmentSelection WHERE [SelectionValue] IS NOT NULL)

     

  • I ran the above 2 queries - no change = 0 rows.

    The issue is not about NULL values here. LEFT OUTER JOIN returns NULL because it does not find a match in the right table for the rows found in LEFT table. Thats why I get NULLs in LEFT OUTER JOIN query. There are 2 rows in ResponseSelectionDictionary table which I know do not exist in AssessmentSelection.

  • did you try

    where isnull(selectionvalue,0) <> 0

    ?

     

  • My apologies to PeterHe, your query actually works. I accidently queried another Server, which has no data. Thanks a Lot Peter!

    I originally thought that only WHERE something = NULL is effected by ANSI NULL setting, but apparently not.

    If I say ANSI NULLS OFF

    Even my query brings back results, without IS NOT NULL and Peter's query works in both cases.

     

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply