1
select Values, REGEXP_COUNT(values,'|') from Products 

Sample Values cell:

Product|Cash|Loan|Customer

Result of select is 27. If count any other char it works, but if I want to count the | it counts the whole string. Why, and how can I count the pipes?

Black Cornail
  • 149
  • 1
  • 9
  • 3
    You shouldn't be storing delimited values like that in a single column to begin with –  Jun 12 '18 at 08:25

2 Answers2

5

That's because | has a special meaning in regular expressions. If you want them to be read as normal characters you need to escape them, using \|.

kabanus
  • 24,623
  • 6
  • 41
  • 74
1

To count specific character you could also use:

SELECT col, LENGTH(col) - LENGTH(REPLACE(col, '|')) AS pipe_count
FROM tab;

DBFiddle Demo

Output:

┌────────────────────────────┬────────────┐
│            COL             │ PIPE_COUNT │
├────────────────────────────┼────────────┤
│ Product|Cash|Loan|Customer │          3 │
└────────────────────────────┴────────────┘
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275