Groups | Blog | Home
all groups > sql server programming > september 2004 >

sql server programming : WHERE - problems with NULL's


Marco Napoli
9/14/2004 10:55:41 PM
I have a WHERE clause when the end user picks to see ALL records or a
selected id records. I am using the below CASE Statement to try to
accomplish this but if there are records with the contact_id = NULL they do
not show up. My CASE statement when ALL is picked it does a comparision that
should be true all the time, contact_id = contact_id. Is there a different
way to accomplish this?

@ALL = 'ALL'
@contact_id = 300

WHERE (CASE WHEN 'ALL' = @ALL THEN contact_id ELSE @id END) = contact_id


Thank you.
Peace in Christ
Marco Napoli
http://www.ourlovingmother.org

David G.
9/15/2004 12:00:39 AM
[quoted text, click to view]

I think the problem is that the statement:
"contact_id = NULL " is never going to be true. Nothing is equal to
NULL. You need to use a statement like this instead:
"contact_id IS NULL"

--
David G.
Toby Herring
9/15/2004 9:42:12 AM
Remember that NULL != NULL, so you'll have to specifically test and correct
for null values.



@ALL = 'ALL'
@contact_id = 300

WHERE (CASE WHEN 'ALL' = @ALL THEN COALESCE(contact_id, -1) ELSE @id END) =
COALESCE(contact_id, -1)


--
Toby Herring
MCDBA, MCSD, MCP+SB
Need a Second Life?
http://secondlife.com/ss/?u=03e0e5b303c234bf08e80ee40119a65e


[quoted text, click to view]

AddThis Social Bookmark Button