Remove Null

  • I am trying to remove Null from my query but fail to do so.

    There are around 20 different dept names in this table. But I only need to display two based on this case statement..

    Select distinct

    Case when P.Dept_Value = 'Accounting' Then 'AccountingPerson'

    When P.Dept_Value = 'ITServices' Then 'ITPerson'

    End Type

    From PesonDept P

    It returns three values

    AccountingPerson

    ITPerson

    NULL

    If I write where P.Dept_Value Is not Null or P.Dept_Value <> Null , it still shows up.

    How should I fix it?

  • sharonsql2013 (5/16/2014)


    I am trying to remove Null from my query but fail to do so.

    There are around 20 different dept names in this table. But I only need to display two based on this case statement..

    Select distinct

    Case when P.Dept_Value = 'Accounting' Then 'AccountingPerson'

    When P.Dept_Value = 'ITServices' Then 'ITPerson'

    End Type

    From PesonDept P

    It returns three values

    AccountingPerson

    ITPerson

    NULL

    If I write where P.Dept_Value Is not Null or P.Dept_Value <> Null , it still shows up.

    How should I fix it?

    You are checking twice...well sort of. 😉

    where P.Dept_Value Is not Null

    You included a second condition which will not work for NULL and as such it will return those with NULL. You cannot use equality OR inequality when checking for NULL. NULL has no value and therefore is not equal to NOR equal to any known value.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I would suggest to restrict your query to only find those 2 values.

    Select distinct

    Case when P.Dept_Value = 'Accounting' Then 'AccountingPerson'

    When P.Dept_Value = 'ITServices' Then 'ITPerson'

    End Type

    From PesonDept P

    WHERE P.Dept_Value IN( 'Accounting', 'ITServices')

    You're getting nulls because you don't have an ELSE on your CASE.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thanks Sean and Luis.

    Sean , I meant to say , I tried both options but did not work.

    Well, Luis , the IN clause works great.

Viewing 4 posts - 1 through 3 (of 3 total)

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