Oracle:一个查询,计算字符串中所有非字母数字字符的出现次数
计算 Oracle 数据库列中字符串中出现的所有非字母数字字符的出现次数的最佳方法是什么。
当试图找到解决方案时,我意识到我有一个与问题无关的查询,但我注意到我可以修改它以希望解决这个问题。我想出了这个:
SELECT COUNT (*), SUBSTR(TITLE, REGEXP_INSTR(UPPER(TITLE), '[^A-Z,^0-9]'), 1)
FROM TABLE_NAME
WHERE REGEXP_LIKE(UPPER(TITLE), '[^A-Z,^0-9]')
GROUP BY SUBSTR(TITLE, REGEXP_INSTR(UPPER(TITLE), '[^A-Z,^0-9]'), 1)
ORDER BY COUNT(*) DESC;
这可以找到第一个非字母数字字符,但我想计算整个字符串中的出现次数,而不仅仅是第一次出现。例如目前,我的查询分析“a(字符串)”会找到一个左括号,但我需要它来找到一个左括号和一个右括号。
What would be the best way to count occurrences of all non alphanumeric characters that appear in a string in an Oracle database column.
When attempting to find a solution I realised I had a query that was unrelated to the problem, but I noticed I could modify it in the hope to solve this problem. I came up with this:
SELECT COUNT (*), SUBSTR(TITLE, REGEXP_INSTR(UPPER(TITLE), '[^A-Z,^0-9]'), 1)
FROM TABLE_NAME
WHERE REGEXP_LIKE(UPPER(TITLE), '[^A-Z,^0-9]')
GROUP BY SUBSTR(TITLE, REGEXP_INSTR(UPPER(TITLE), '[^A-Z,^0-9]'), 1)
ORDER BY COUNT(*) DESC;
This works to find the FIRST non alphanumeric character, but I would like to count the occurrences throughout the entire string, not just the first occurrence. E. g. currently my query analysing "a (string)" would find one open parenthesis, but I need it to find one open parenthesis and one closed parenthesis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一个不起眼的 Oracle TRANSLATE 函数可以让您代替正则表达式来执行此操作:
There is an obscure Oracle TRANSLATE function that will let you do that instead of regexp:
试试这个:
Try this:
正如您所发现的,最好的选择是使用 PL/SQL 过程。我认为没有任何方法可以创建一个正则表达式,它会像您期望的那样返回多个计数(至少在 Oracle 中不是这样)。
解决此问题的一种方法是使用递归查询单独检查每个字符,这可用于为找到的每个字符返回一行。以下示例适用于单行:
The best option, as you discovered is to use a PL/SQL procedure. I don't think there's any way to create a regex expression that will return multiple counts like you're expecting (at least, not in Oracle).
One way to get around this is to use a recursive query to examine each character individually, which could be used to return a row for each character found. The following example will work for a single row: