Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 when column has NULL value

Author  Topic 

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2009-03-09 : 18:10:07
Greetings

I have table ShiftMinutes

ShiftMinuteID int
CommodityID int
DefinitionValue int
DefinitionDate datetime
BranchID int
UserDate datetime
UserName varchar

DefinitionValue is the sum minutes for a branch, commodity. Sometime thought a shift could handle any kind of commodity and decided to make it a nullable field. Now when I have @CommodityID as NULL paramater and do the following in WHERE clause on ShiftMinutes table WHERE CommodityID = @CommodityID I get no result. But when I do WHERE (CommodityID = @CommodityID OR commodityid IS NULL) I get the result I want. But is this a good design or did I just stumble on a solution, which I don't like I want to know why it worked and why it did not.

Thanks !!

guptam
Posting Yak Master

161 Posts

Posted - 2009-03-09 : 18:30:13
You cannot compare NULL to a field. That is why you have to use commodityis IS null. NULL is a special value and cannot be compared. Your solution is fine..

--
Mohit K. Gupta
B.Sc. CS, Minor Japanese
MCITP: Database Administrator
MCTS: SQL Server 2005
http://sqllearnings.blogspot.com/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-10 : 03:10:17
quote:
Originally posted by guptam

You cannot compare NULL to a field. That is why you have to use commodityis IS null. NULL is a special value and cannot be compared. Your solution is fine..

--
Mohit K. Gupta
B.Sc. CS, Minor Japanese
MCITP: Database Administrator
MCTS: SQL Server 2005
http://sqllearnings.blogspot.com/


You can compare if you set ANSI NULL SETTING to off. then NULL will be regarded as a value too
Go to Top of Page
   

- Advertisement -