Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Remove Null Expand / Collapse
Author
Message
Posted Friday, May 16, 2014 3:02 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 2:14 PM
Points: 211, Visits: 620
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?

Post #1571926
Posted Friday, May 16, 2014 3:05 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:57 PM
Points: 13,058, Visits: 11,884
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1571930
Posted Friday, May 16, 2014 3:10 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:18 PM
Points: 3,301, Visits: 7,125
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1571932
Posted Friday, May 16, 2014 3:42 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 2:14 PM
Points: 211, Visits: 620
Thanks Sean and Luis.
Sean , I meant to say , I tried both options but did not work.

Well, Luis , the IN clause works great.

Post #1571941
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse