How to count the number of occurrences of a character in an Oracle varchar value?(如何计算 Oracle varchar 值中某个字符出现的次数?)
问题描述
如何计算 varchar2 字符串中字符 - 的出现次数?
How can I count number of occurrences of the character - in a varchar2 string?
示例:
select XXX('123-345-566', '-') from dual;
----------------------------------------
2
推荐答案
给你:
select length('123-345-566') - length(replace('123-345-566','-',null))
from dual;
从技术上讲,如果你要检查的字符串只包含你要计数的字符,上面的查询将返回NULL;以下查询将在所有情况下给出正确答案:
Technically, if the string you want to check contains only the character you want to count, the above query will return NULL; the following query will give the correct answer in all cases:
select coalesce(length('123-345-566') - length(replace('123-345-566','-',null)), length('123-345-566'), 0)
from dual;
coalesce 中的最后一个 0 捕捉您在空字符串中计数的情况(即 NULL,因为在 ORACLE 中 length(NULL) = NULL).
The final 0 in coalesce catches the case where you're counting in an empty string (i.e. NULL, because length(NULL) = NULL in ORACLE).
这篇关于如何计算 Oracle varchar 值中某个字符出现的次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何计算 Oracle varchar 值中某个字符出现的次数?
基础教程推荐
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 无法解决整理冲突 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
