0

so given the table:

id     | names
===============
1        {John, , Wayne}
2        {Luke, Harold, }
3        {Bill}
4        {Will, , }

They don't have a standard and some values may come empty ( for example {Will, , }). I tried:

SELECT array_length(names, 1)
FROM nameTable

But I get this:

names
======
3
3
1
3

and I want it to return:

names
======
2
2
1
1

So I need something which gives me the length only of the populated fields (empty spaces like ' ') shouldn't be counted.

2
  • Are you using MySQL or Postgresql? Commented Dec 11, 2019 at 12:20
  • Postgresql ..... Commented Dec 11, 2019 at 12:21

1 Answer 1

1

You can remove the NULL values and then count:

array_length(array_remove(names, NULL), 1)

For one-dimensional arrays, I find that cardinality() is convenient:

cardinality(array_remove(names, NULL))
Sign up to request clarification or add additional context in comments.

2 Comments

This is it. it works! But instead of Null I put ' '. Thank you!
@SlimKode . . . I assumed the unseen value was NULL, but you can put in any value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.