1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-02 22:58:01 +00:00
Files
til/postgres/word-count-for-a-column.md
2015-05-09 11:45:47 -05:00

22 lines
560 B
Markdown

# Word Count for a Column
Assuming I have a database with a posts table:
```sql
> 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:
```sql
> select sum(array_length(regexp_split_to_array(content, '\s+'), 1)) from posts where id = 1;
sum
-----
7
```
[source](http://blog.lingohub.com/2013/07/sql-word-count-character-count-postgres/)