views:

60

answers:

3

How to manage NULL values in numeric fields returned by cursor in Select stament, to manage efficienly aritmetic operations ?

+3  A: 
  1. Don't use cursors.

  2. If you must (really?), you can use the ISNULL function:

    SELECT ISNULL(fieldname, 0)
    

will give you a "0" (zero) instead of NULL.

marc_s
+3  A: 
ISNULL(value, replacement)

will replace value with replacement if value IS NULL

http://msdn.microsoft.com/en-us/library/ms184325.aspx

Nick Spiers
+1  A: 

Assuming you cannot avoid a cursor in the first place, I don't understand why a NULL would be handled much differently in a variable than you would in a query - there is INSULL, COALESCE, CASE WHEN etc.

One interesting thing:

DECLARE @v as int -- initialized to NULL

{ -- loop through a cursor
FETCH NEXT INTO @v
}

You won't be able to necessarily distinguish an uninitialized @v from when the last row's @v setting was NULL.

Cade Roux