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
  • 21,258
  • 32
  • 119
  • 207

1 Answers1

1

One method is the difference of lengths:

select (length(col) - length(replace(col, 'X', ''))) as NumX
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786