Home > OS >  Determining the length of a comma separated string
Determining the length of a comma separated string

Time:05-27

Suppose I have a column that has a value of a comma separated string of various lengths:

 locations            |  city  |  county |  state
 --------------------- -------- --------- --------
 A, B, C              |        |         |     
 --------------------- -------- --------- --------     
 A, B                 |        |         |
 --------------------- -------- --------- --------
 A, B, C              |        |         |

So, the length varies. I want to know what the length of the values separated by comma would be. For example, 'A, B, C' has a lenght of 3, 'A, B' has a length of 2. How do I find that length using PostgreSQL?

CodePudding user response:

You can split the string into array:

select cardinality(string_to_array('a,b,c', ','));

 cardinality
-------------
           3
(1 row)
  • Related