views:

180

answers:

4

Hi all,

is there a function in SQL Server 2005 that returns NULL [or a boolean value] if any of the arguments (of any type) is NULL, which would save me from writing IF a IS NULL OR b IS NULL OR c IS NULL ....

+1  A: 

No, the closest you an get is NULLIF(), but that's not what you want. I'd just stick to using the OR statement here.

Dave Markle
Actually, NULLIF returns NULL if the two input-parameters are the same. I don't see any way to achieve this with cascaded NULLIFs.
ercan
Thanks. I misspoke.
Dave Markle
+2  A: 

Here is a moderately unpleasant way of doing it:

set ansi_nulls off
if (null in (a, b, c, d, e) print 'got a null'
set ansi_nulls on
Alex K.
~shudders at the implications~
Jeremy
A stern caution from Microsoft that NULL can produce unexpected results in this scenario: http://msdn.microsoft.com/en-us/library/ms177682.aspx
John K
+1 for admitting that this is moderately unpleasant :)
ercan
A: 

Since NULLs propogate you could do:

(cola + colb + colc) is null

assuming all compatible data types

Jeremy
That's why I put the constraint **of any type** in the post ;)
ercan
Maybe you could coerce them into a single type, since the values themselves don't matter for the test. Maybe a nullable bit?
tloflin
A: 

How about ...

SELECT
CASE WHEN NULLIF(ISNULL(@testA, 1), @testA) 
        + NULLIF(ISNULL(@testB, 1), @testB) 
        + NULLIF(ISNULL(@testC, 1), @testC) > 0
    THEN 'Got NULL'
    ELSE 'NO NULL'
END
al
This takes care of the **"of any type"** constraint, but for the sake of readability, I would prefer `@testA IS NULL OR @testB IS NULL OR @testC IS NULL`
ercan