Finding the count of characters and numbers in a string

前端 未结 2 1510
-上瘾入骨i
-上瘾入骨i 2020-12-05 16:22

Hi I have a table test as below

NAME
---------
abc1234
XYZ12789
a12X8b78Y9c5Z

I try to find out the count of number of numbers and characte

相关标签:
2条回答
  • 2020-12-05 17:05

    If I understand correctly you are using Oracle PLSQL, and as far as I know, there isn't any "built-in" method (in PLSQL) that counts the number of digits/characters in a string.

    But, you can do the following to count characters:
    select LENGTH(REGEXP_REPLACE('abcd12345','[0-9]')) from dual

    and digits:
    select LENGTH(REGEXP_REPLACE('abcd12345','[a-zA-Z]')) from dual

    Or, in your case:

    select name,
    LENGTH(REGEXP_REPLACE(name,'[a-zA-Z]','')) as num_count,
    LENGTH(REGEXP_REPLACE(name,'[0-9]','')) as char_count,
    from test6;
    

    For Bill the Lizard:
    My answer was tested on Oracle 11g and it works just fine!
    If you decide to delete my answer again, please be kind enough to add a comment that explains why. I was also looking for you in the chat rooms...

    0 讨论(0)
  • 2020-12-05 17:27

    @alfasin answer is good, but if you're using 11g then it can get simpler:

    select name,
    REGEXP_count(name,'\d') as num_count,
    REGEXP_count(name,'[a-zA-Z]') as char_count,
    from test6;
    
    0 讨论(0)
提交回复
热议问题