SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Remove Null


Remove Null

Author
Message
sharonsql2013
sharonsql2013
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1302 Visits: 1265
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?
Sean Lange
Sean Lange
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63634 Visits: 17966
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.

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)
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42538 Visits: 19839
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
sharonsql2013
sharonsql2013
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1302 Visits: 1265
Thanks Sean and Luis.
Sean , I meant to say , I tried both options but did not work.

Well, Luis , the IN clause works great.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search