How to count the number of occurrences of a character in an Oracle varchar value?

前端 未结 9 1208
醉梦人生
醉梦人生 2020-12-01 07:35

How can I count number of occurrences of the character - in a varchar2 string?

Example:

select XXX(\'123-345-566\', \'-\') from dual;
--         


        
相关标签:
9条回答
  • 2020-12-01 08:07

    here is a solution that will function for both characters and substrings:

    select (length('a') - nvl(length(replace('a','b')),0)) / length('b')
      from dual
    

    where a is the string in which you search the occurrence of b

    have a nice day!

    0 讨论(0)
  • 2020-12-01 08:10

    Here's an idea: try replacing everything that is not a dash char with empty string. Then count how many dashes remained.

    select length(regexp_replace('123-345-566', '[^-]', '')) from dual
    
    0 讨论(0)
  • 2020-12-01 08:16

    REGEXP_COUNT should do the trick:

    select REGEXP_COUNT('123-345-566', '-') from dual;
    
    0 讨论(0)
提交回复
热议问题