Length returns NULL ?

Started by Marcos Pegoraroalmost 2 years ago2 messagesgeneral
Jump to latest
#1Marcos Pegoraro
marcos@f10.com.br

There are some functions called ...length, but only array_length returns
NULL on empty array, why ?

select array_length('{}'::text[],1), -->NULL
jsonb_array_length('[]'), -->0
bit_length(''), -->0
octet_length(''), -->0
length(''), -->0
char_length(''), -->0
length(B''); -->0

I know, it is documented, but the question is, why does it work differently
?

array_length ( anyarray, integer ) → integer
Returns the length of the requested array dimension. (Produces NULL instead
of 0 for empty or missing array dimensions.)

array_length(array[1,2,3], 1) → 3
array_length(array[]::int[], 1) → NULL
array_length(array['text'], 2) → NULL

#2Erik Wienhold
ewie@ewie.name
In reply to: Marcos Pegoraro (#1)
Re: Length returns NULL ?

On 2024-06-05 19:50 +0200, Marcos Pegoraro wrote:

There are some functions called ...length, but only array_length returns
NULL on empty array, why ?

Because the empty array has zero dimensions[1]/messages/by-id/603c8f070811061951u16034c3fk5dfaa493a6739a24@mail.gmail.com. But now I'm wondering
why array_ndims returns NULL instead of zero for empty arrays.

[1]: /messages/by-id/603c8f070811061951u16034c3fk5dfaa493a6739a24@mail.gmail.com

--
Erik