number of not null arguments

Started by Murat Kabilovover 15 years ago2 messagesgeneral
Jump to latest
#1Murat Kabilov
kabilovmf@gmail.com

Hello,

Is there a function which returns number of not null arguments?

SELECT notnull_count(1, 1, NULL, NULL)
notnull_count
---------------
2

SELECT notnull_count(ARRAY[1,2,3], ARRAY[10,20,30], NULL, ARRAY[NULL])
notnull_count
---------------
3

Thanks

--
Murat Kabilov

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Murat Kabilov (#1)
Re: number of not null arguments

Hello

this function doesn't exists, but you can you to write (min PostgreSQL 8.4)

create or replace function notnull_count(variadic anyarray) returns
int as $$select count(x)::int from unnest($1) g(x)$$ language sql;

it working just for scalar types:

pavel=# SELECT notnull_count(1, 1, NULL, NULL); notnull_count
───────────────
2
(1 row)

it doesn't working for arrays, but you can to little bit modify query

pavel=# SELECT notnull_count(array_upper(ARRAY[1,2,3],1),
array_upper(ARRAY[10,20,30],1), NULL, array_upper(ARRAY[NULL],1));
notnull_count
───────────────
3
(1 row)

next (but general solution) is custom function in C - it can be very simple

Regards

Pavel Stehule

2010/11/26 Murat Kabilov <kabilovmf@gmail.com>:

Show quoted text

Hello,
Is there a function which returns number of not null arguments?
SELECT notnull_count(1, 1, NULL, NULL)
 notnull_count
---------------
             2
SELECT notnull_count(ARRAY[1,2,3], ARRAY[10,20,30], NULL, ARRAY[NULL])
 notnull_count
---------------
             3
Thanks
--
Murat Kabilov