显示第二次出现字母 A 的所有员工的姓名,而不使用 like 运算符

Display all the names of the employees whose names having letter A as 2nd occurrence without using like operator

对于上述查询,我​​使用 regexp_count 但在 SQL 命令行中我得到 regexp_count :invaild identifier;

select * from table_name WHERE (REGEXP_COUNT(column_name, 'A')) >2;

这个查询有效吗?

我不确定你真正想要什么; return 第二个字母是“a”的名字,还是其中有两个或更多字母“a”的名字。

总之,选一个你觉得合适的吧。

SQL> create table test as
  2    (select 'saritha' col  from dual union all
  3     select 'mamatha'      from dual union all
  4     select 'vaisnavi'     from dual union all
  5     select 'sai'          from dual union all
  6     select 'vijaya'       from dual union all
  7     select 'kumar'        from dual
  8    );

Table created.

2 个或更多字母“a”:

SQL> select col
  2  from test
  3  where regexp_count(col, 'a') >= 2;

COL
--------
saritha
mamatha
vaisnavi
vijaya

第二个字母是“a”:

SQL> select col
  2  from test
  3  where substr(col, 2, 1) = 'a';

COL
--------
saritha
mamatha
vaisnavi
sai

SQL>

在不支持 REGEXP_COUNT 函数的 Oracle 10g 上,一种选择是将所有字母 a 替换为空字符串(基本上,您将删除所有字母 a) 并获取全列长度与“替换”列长度之差为 >= 2 的行。像这样:

SQL> select col
  2  from test
  3  where length(col) - length(replace(col, 'a', '')) >= 2;

COL
--------
saritha
mamatha
vaisnavi
vijaya

SQL>