How to count the number of terms in a table with postgresql?

58 Views Asked by At

alldp charcter varraying

"K409,K358,K353,L059,R100,L050,K610,K352,K429,C181,D129,K565,T8558,K560,K402,A045,K661,I841,I848,Z433"

I can count them. here is 20.

But if I want to write a statement in Postgres to get 20, how should I do?

1

There are 1 best solutions below

0
On BEST ANSWER

You can split the string into an array and then count the elements in the resulting array:

select array_length(string_to_array(the_column, ','), 1)
from the_table;

But you should really consider fixing your data model. Storing comma separated values in a column is almost always a bad idea.