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 2000 Forums
 SQL Server Development (2000)
 ISNULL is not null??

Author  Topic 

Peter Dutch
Posting Yak Master

127 Posts

Posted - 2002-10-10 : 08:50:12
I often use something like this:

...
WHERE SomeField = ISNULL(@theParameter,SomeField)

to simulate optional parameters. (if the @theParameter is not passed, it compares the comlumns to itself).

This works great, BUT not if the SomeField contains NULL.

If SomeField contains NULL and @theParameter contains NULL it doesn't returns the record!!! I used the SET ANSI_NULLS ON but that didn't help.

In my opinion it should return the record. Any ideas?

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2002-10-10 : 08:54:51
where isnull(somefield,'somefield is null') = coalesce(@theParameter,somefiled,'somefield is null')

Jay White
{0}
Go to Top of Page

Peter Dutch
Posting Yak Master

127 Posts

Posted - 2002-10-10 : 08:57:46
Yeah, thought of that, but thanks for your reply!

I was hoping for a different solution, and/or an explanation on why this happens.

NULL equals NULL if you ask me...

Go to Top of Page

davidpardoe
Constraint Violating Yak Guru

324 Posts

Posted - 2002-10-10 : 09:03:48
NULL means unknown hence if you try and compare unknown to unknown the result mut be unknown

If you really do want to match NULL with NULL then you could use..

WHERE ISNULL(SomeField,'') = ISNULL(@theParameter,ISNULL(SomeField,''))

ie. replace NULLs with an empty string which does match another empty string

============================
Chairman of
The NULL Appreciation Society
"Keep NULLs as NULL"

Edited by - davidpardoe on 10/10/2002 09:04:15
Go to Top of Page

Peter Dutch
Posting Yak Master

127 Posts

Posted - 2002-10-10 : 09:10:39
Well the strange thing is, when you do (assuming SomeField contains NULL):


SELECT * FROM MyTable
WHERE SomeField IS NULL

It returns the record, but when you do (assuming Somefield and @Param contain NULL)

SELECT * FROM MyTable
WHERE SomeField = ISNULL(@Param,SomeField)

it doesn't returns the record. That doesn't make sense to me. Why would it handle NULLs differently??

Go to Top of Page

davidpardoe
Constraint Violating Yak Guru

324 Posts

Posted - 2002-10-10 : 09:13:42
When you use the following..

SELECT * FROM MyTable
WHERE SomeField IS NULL

it is specifically checking if the field is null hence the condition is true.

If you use..

SELECT * FROM MyTable
WHERE SomeField = ISNULL(@Param,SomeField)

then your condition is effectively NULL = NULL which is FALSE due to my previous post (ie. it always unknown if unknown equals unknown!)

============================
Chairman of
The NULL Appreciation Society
"Keep NULLs as NULL"
Go to Top of Page

Peter Dutch
Posting Yak Master

127 Posts

Posted - 2002-10-10 : 09:16:57
Ok, thanks for your explanation. Makes sense now.

Guess I'll have to live with it..

Thanks aigain!

Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2002-10-10 : 09:20:06
Don't know if this helps, it was an example in a SQL book, maybe one of Joe Celko's:

A table holding data about people, name, height, weight, hair color. Two people, Joe and Ginny, have Null in the hair color column (unknown hair color) Does that mean they have the SAME color hair? How can you prove it?

Basically, that's why a Null cannot equal another Null.

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-10-10 : 09:24:18
because null = null evaluates to null which is handled as false as does null = 1 and 1 = null.

try these - the sort of thing you should try whenever you change version or service pack.
I suspect the 'in' handling has changed at some point.

select 1 where null = null
select 1 where null <> null
select 1 where not null = null
select 1 where not null <> null
select 1 where 1 = null
select 1 where 1 <> null
select 1 where not 1 = null
select 1 where not 1 <> null
select 1 where 1 in (1,null)
select 1 where 1 not in (1,null)
select 1 where 1 not in (2,null)
select 1 where null in (1,null)
select 1 where null not in (1,null)
select 1 where null not in (2,null)


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -