tags:

views:

43

answers:

2
+2  Q: 

NULL comparison

Hello all,

There is a query:

UPDATE MyTable SET nvarchar1 = 'blahblah'
WHERE Id = '096fe792-7313-416f-b3c8-327f46be73b6' AND nvarchar1 <> 'blablah'

It doesn't work, when nvarchar1 is NULL. How should I change it to make it work?

  1. It is important to me don't execute update, if value has not been really changed.
  2. I don't know column type. It can be not only nvarchar, but ntext, integer or float-point number as well.

Regards,

+2  A: 
UPDATE MyTable SET nvarchar1 = 'blahblah'
WHERE Id = '096fe792-7313-416f-b3c8-327f46be73b6' 
AND (nvarchar1 IS NULL 
OR nvarchar1 <> 'blablah')

Do you mean this?

JanW
Yeah, the only correct answer. I forgot about IS NULL, since ISNULL doesn't work obviously. Thank you.
noober
Why does ISNULL not work?
Joe Stefanelli
1. Since I don't know column type (as it referenced in point 2) I don't know, what default value should be used. It is '' for nvarchar, 0 for numbers etc. And I have to have different queries, depending on column type.2. It is worse, even if the type would be nvarchar always, the predefined default value ('') can be the same as the value that's being set. For instance, if your query is erasing previous value with empty string, it would be UPDATE MyTable SET nvarchar1 = '' WHERE Id = '096fe792-7313-416f-b3c8-327f46be73b6' AND isnull(nvarchar1,'') <> ''
noober
Ah, you are correct. I was blinded by focusing just on your specific example.
Joe Stefanelli
A: 
UPDATE MyTable SET nvarchar1 = 'blahblah'
WHERE Id = '096fe792-7313-416f-b3c8-327f46be73b6' AND isnull(nvarchar1,'') <> 'blablah'
GSerg