1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Word Count for a Column as a postgres til.

This commit is contained in:
jbranchaud
2015-05-09 11:43:18 -05:00
parent 1bf6290c2a
commit 6695f37cbb
2 changed files with 22 additions and 0 deletions

View File

@@ -44,6 +44,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
- [Timestamp Functions](postgres/timestamp-functions.md)
- [Turning Timing On](postgres/turning-timing-on.md)
- [Word Count for a Column](postgres/word-count-for-a-column.md)
### rails

View File

@@ -0,0 +1,21 @@
# 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](http://blog.lingohub.com/2013/07/sql-word-count-character-count-postgres/)