0

In Postgres/SQL, how can I get the count of a character in a column?

For example, I want to run a query which will return the number of times "X" appears in a column that has the value "XXX" - and it should return 3.

Henley
  • 19,640
  • 29
  • 114
  • 195

1 Answers1

1

One method is the difference of lengths:

select (length(col) - length(replace(col, 'X', ''))) as NumX
Gordon Linoff
  • 1,198,228
  • 53
  • 572
  • 709