Home / Blog / Dev Bit: SQL word count & character count with Postgres

Dev Bit: SQL word count & character count with Postgres

Developer resources
Helmut Juskewycz
CEO & Founder of LingoHub

Last updated

7/24/2013

Read time

1 min

Best for

Developers

You create - Lingohub localizes

At LingoHub we utilize Postgres and Elasticsearch to handle our large text base. Is there a way to do an SQL word count for our translations? There is. I wanted to share with you two SQL queries for character count and word count of a text column:


# Character count
select sum(length(YOUR_COLUMN)) from YOUR_TABLE;

# Word count
select sum(array_length(regexp_split_to_array(YOUR_COLUMN, '\s'),1)) from YOUR_TABLE;

Related articles