Today I Learned

hashrocket A Hashrocket project

Word Count for a Column

Assuming I have a database with a posts table:

> select * from posts where id = 1;
 id |  title   |              content               
----+----------+------------------------------------
  1 | My Title | This is the content of my article. 

I can compute the word count of the content of a given post like so:

> select sum(array_length(regexp_split_to_array(content, '\s+'), 1)) from posts where id = 1;
 sum 
-----
   7 

source

See More #sql TILs
Looking for help? Hashrocket developers believe that data quality is as important as code quality. We enjoy all the challenges of relational databases, from finding the fastest index, to structuring data to fit the needs of an application. We're eager to share our experiences; check out PG Casts, our series of free PostgreSQL screencasts.