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?

a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843
Black Cornail
  • 139
  • 1
  • 9

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
  • 22,925
  • 6
  • 32
  • 68
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
  • 139,860
  • 19
  • 198
  • 228