all groups > sql server programming > september 2005 >
You're in the

sql server programming

group:

selecting rows with null values



selecting rows with null values Meetu
9/27/2005 11:41:56 PM
sql server programming: query = UPDATE
OrderFormLineItems
SET
cc_status = null,
cc_auth_number = null,
cc_settle_number = null
WHERE
ordergroup_id = '@id'
and
cc_status != 'SOK'

what to you think it does if the cc_status column equals Null? Null
does not equal 'SOK' right? Well it never select the records where
cc_status is Null. I guess I'm going to have to recode it to "add
(cc_status = null or cc_status != 'SOK')" Yuck! Please let me know if
anyone has a more graceful way of handling this.
Re: selecting rows with null values David Portas
9/28/2005 12:00:00 AM
NULLs are different! The comparison operators do not work with NULLs as they
do with real values. Avoid NULLs where you can. Read about NULLs and
three-value logic in Books Online:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/acdata/ac_8_qd_02_8pwy.asp

UPDATE OrderFormLineItems
SET cc_status = NULL,
cc_auth_number = NULL,
cc_settle_number = NULL
WHERE ordergroup_id = '@id'
AND (cc_status <> 'SOK'
OR cc_status IS NULL) ;

--
David Portas
SQL Server MVP
--

RE: selecting rows with null values John Bell
9/28/2005 12:00:02 AM
Hi

You may want to read Itzik's article "Don’t Avoid the UNKNOWN" in the
September issue of SQL Server magazine.
http://www.windowsitpro.com/Authors/AuthorID/638/638.html

You can not say cc_status = null use

(cc_status IS NULL OR cc_status <> 'SOK')

John

[quoted text, click to view]
Re: selecting rows with null values Razvan Socol
9/28/2005 12:04:39 AM
Hi, Meetu

We can say that "NULL is not equal to 'SOK'", but also that "NULL is
not different to 'SOK'". In fact, NULL is not equal to anything,
because it means "unknown". We can even say that "NULL is not equal to
NULL". For more informations, see:
http://msdn.microsoft.com/library/en-us/acdata/ac_8_qd_06_7xir.asp

Regarding your query, the following solutions are possible:
1. Use SET ANSI_NULLS OFF (not recommended)
2. Use: ISNULL(cc_status,'')<>'SOK'
3. Use: cc_status IS NULL or cc_status<>'SOK'

Razvan
RE: selecting rows with null values R.D
9/28/2005 12:16:04 AM
yup
that's right: but cc_status = null may not work use cc_status is null
for update = null should be used
for select or where use is null as per ANSI-
92 standard
OR
SET ANSI_NULLS OFF
Regards
R.D
[quoted text, click to view]



[quoted text, click to view]
Re: selecting rows with null values Meetu
9/28/2005 4:48:34 AM
Thank you David ,John,Razvan and R.D. for replying.

I find the use of isnull(cc_status,'') useful, but I didnt understand
one thing why Razvan mentioned that Set ansi_null off is not
recommended? Razvan can you elaborate on this one? Thanks
Re: selecting rows with null values John Bell
9/28/2005 6:09:02 AM
Hi

From BOL:
The SQL-92 standard requires that an equals (=) or not equal to (<>)
comparison against a null value evaluates to FALSE. When SET ANSI_NULLS is
ON, a SELECT statement using WHERE column_name = NULL returns zero rows even
if there are null values in column_name. A SELECT statement using WHERE
column_name <> NULL returns zero rows even if there are nonnull values in
column_name.

When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To (<>) comparison
operators do not follow the SQL-92 standard. A SELECT statement using WHERE
column_name = NULL returns the rows with null values in column_name. A SELECT
statement using WHERE column_name <> NULL returns the rows with nonnull
values in the column. In addition, a SELECT statement using WHERE column_name
<> XYZ_value returns all rows that are not XYZ value and that are not NULL.

There are several features that will not work when ANSI_NULLS is off such as
distributed queries and indexed views.

John

[quoted text, click to view]
AddThis Social Bookmark Button