SQL Query to check if 40 columns in table is null SQL Query to check if 40 columns in table is null sql sql

SQL Query to check if 40 columns in table is null


maybe with a COALESCE

SELECT * FROM table WHERE coalesce(col1, col2, col3, ..., colN) IS NULL


where c1 is null and c2 is null ... and c60 is null

shortcut using string concatenation (Oracle syntax):

where c1||c2||c3 ... c59||c60 is null


Are you trying to find out if a specific set of 60 columns are null, or do you just want to find out if any 60 out of the 100 columns are null (not necessarily the same 60 for each row?)

If it is the latter, one way to do it in oracle would be to use the nvl2 function, like so:

select ... where (nvl2(col1,0,1)+nvl2(col2,0,1)+...+nvl2(col100,0,1) > 59)

A quick test of this idea:

select 'dummy' from dual where nvl2('somevalue',0,1) + nvl2(null,0,1) > 1

Returns 0 rows while:

select 'dummy' from dual where nvl2(null,0,1) + nvl2(null,0,1) > 1

Returns 1 row as expected since more than one of the columns are null.