dahui
dahui

Reputation: 2166

How to get occurrences of string lengths for a column of strings in postgresql

Given an input of a text/string column, I want to compute the lengths of the strings in the columns, and the counts for each length.

E.g. a column with the strings:

'Ant'
'Cat'
'Dog'
'Human'
''
NULL
'A human'

Would give:

0 : 1
3 : 3
5 : 1
7 : 1

NOTE: the null string hasn't been counted as a 0 character string, but ignored.

Upvotes: 1

Views: 24

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269443

length() comes to mind:

select length(col), count(*)
from t
where col is not null
group by length(col)
order by length(col);

Upvotes: 1

Related Questions