Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse of COALESCE

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 ....

like image 861
ercan Avatar asked Apr 21 '10 13:04

ercan


People also ask

Is NVL same as coalesce?

The obvious differences are that coalesce will return the first non-null item in its parameter list whereas nvl only takes two parameters and returns the first if it is not null, otherwise, it returns the second. It seems that NVL may just be a 'Base Case" version of coalesce.

Is coalesce same as Ifnull?

COALESCE is useful when you have unknown number of values that you want to check. IFNULL is useful when you select columns and know that it can be null but you want to represent it with a different value. Therefore, the two functions are vastly different.

How do you replace coalesce in SQL?

Solution – To replace the null values with another values we can use the COALESCE() function in SQL. The COALESCE() function returns the first Non-Null value in a list. This query returns 5 as this is the first Non-Null value in this list.

Which is better coalesce or Isnull?

COALESCE and ISNULL advantage that COALESCE has over ISNULL is that it supports more than two inputs, whereas ISNULL supports only two. Another advantage of COALESCE is that it's a standard function (namely, defined by the ISO/ANSI SQL standards), whereas ISNULL is T-SQL–specific.


3 Answers

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
like image 72
Alex K. Avatar answered Nov 15 '22 22:11

Alex K.


Since NULLs propogate you could do:

(cola + colb + colc) is null

assuming all compatible data types

like image 40
Jeremy Avatar answered Nov 16 '22 00:11

Jeremy


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.

like image 30
Dave Markle Avatar answered Nov 16 '22 00:11

Dave Markle