繁体   English   中英

查找字符串中的字符数和数字

[英]Finding the count of characters and numbers in a string

嗨,我有一个表测试如下

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

我试着找出字符串中数字和字符数的计数

select name,length(replace(translate(lower(name),'abcdefghijklmnopqrstuvwxyz',' '),'      ','')) as num_count,
length(replace(translate(name,'1234567890',' '),' ','')) as char_count
from test6;

它执行正常给出输出

NAME    NUM_COUNT   CHAR_COUNT
abc1234         4       3
XYZ12789        5       3
a12X8b78Y9c5Z   7       6

但我的问题是没有任何选项,不给人手动abcdefghijklmnopqrstuvwxyz1234567890

@alfasin答案很好,但是如果你使用11g那么它可以变得更简单:

select name,
REGEXP_count(name,'\d') as num_count,
REGEXP_count(name,'[a-zA-Z]') as char_count,
from test6;

如果我理解你正在使用Oracle PLSQL,据我所知,没有任何“内置”方法(在PLSQL中)计算字符串中的数字/字符数。

但是,您可以执行以下操作来计算字符:
select LENGTH(REGEXP_REPLACE('abcd12345','[0-9]')) from dual

和数字:
select LENGTH(REGEXP_REPLACE('abcd12345','[a-zA-Z]')) from dual

或者,在您的情况下:

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

比尔蜥蜴:
我的答案在Oracle 11g上进行了测试,它运行得很好!
如果您决定再次删除我的答案,请善意添加解释原因的评论。 我也在聊天室里找你...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM