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)
 'where' problem...

Author  Topic 

dlorenc
Posting Yak Master

172 Posts

Posted - 2008-10-10 : 14:37:58
what's wrong with my where logic?

I want to select comments where the commenttype is not 'Confidential' or 'Tickle'. commenttype is (nvarchar(20),null)...and charter 197 has several records with commenttypes = NULL....???

the resulting set is empty...

here is the query:

SELECT *
FROM CharterComments
WHERE CharterComments.Charter_ID = 197 and (CharterComments.CommentType <> 'CONFIDENTIAL' or CharterComments.CommentType <> 'Tickle')

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-10 : 14:40:12
You need to use an AND instead of OR.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2008-10-10 : 14:41:57
the 'and' between confidential and tickle still return an empty set...

I was thinking something weird with nvarchar and nulls?...

infact..if I simplify the where, I still dont understand why the set is empty...if I remove the 'and' I get about 10 records....all with commenttype = NULL...

WHERE CharterComments.Charter_ID = 197 and CharterComments.CommentType <> 'CONFIDENTIAL'
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2008-10-10 : 15:15:54
I ran an

UPDATE CharterComments SET CommentType = 'OPEN' where CommentType is Null

and the where works...go figgure..*sigh*...luckily this is a tiny database...this makes absolutely no sense to me...*sigh*...

maybe I discovered a new sql rule?.."If nvarchar type field is compared with text values, nulls are discarded as they are, by definition, not having a text value."...rotf...

As I tell my kids - "This is a mystery of life we may never understand..."

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-10 : 15:27:13
Yes NULLs would be eliminated in your query, I missed that part in your original post.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2008-10-10 : 16:40:18
mm...so 'nulls' would have to be explicitly included?..

and commenttype is null...

??
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-10 : 17:00:30
[code]SELECT *
FROM CharterComments
WHERE CharterComments.Charter_ID = 197 and isnull(CharterComments.CommentType ,'') not in ('CONFIDENTIAL' ,'Tickle')
[/code]
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2008-10-10 : 17:20:02
again, thank you all for your help...I just never thought a null value had to be explicitly included in the query....go figgure...
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-10 : 17:24:01
quote:
Originally posted by dlorenc

again, thank you all for your help...I just never thought a null value had to be explicitly included in the query....go figgure...



It's one of the reasons why people try to design a system with very few nullable columns.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

dlorenc
Posting Yak Master

172 Posts

Posted - 2008-10-10 : 17:36:52
Beginning SQL Server 2005 for Developers By Robin Dewson

A NULL value means there is absolutely nothing entered in that column - no data at all. A column with a NULL value is a special data state, with special meaning.

If a field has a NULL value, no data has been inserted into the column. this also means that you have to perform special function statements within any T-Sql code to test for this value. Take the example of a column defined to hold characters, but where one of the rows has a NULL value within it. If you completed a SQL function that carried out string manipulation, then the row with the NULL value would cause the row not to be included in the function without special processing.

So what advantages are there to allowing NULL values? The largest advantage is that you know nothing has been entered in it...another advantage is teh small space that a NULL column takes up. To be precise, it takes up no space whatsoever, again, unlike a 0 or a single space.

************

now I understand the whole concept of 'NULL' much better...thank you all!
Go to Top of Page
   

- Advertisement -