May 16, 2014 at 3:02 pm
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?
May 16, 2014 at 3:05 pm
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/
May 16, 2014 at 3:10 pm
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.
May 16, 2014 at 3:42 pm
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