如何在oraclesql中找到col包含字母、数字或特殊字符(连字符、撇号和空格除外)的行

jdg4fx2g  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(463)

我必须找到这样的名字:robert@jr23 (必须有字母表、除连字符(-)、撇号(')和空格以外的任何特殊字符或数字)。
我是这样做的:

select * from test where REGEXP_LIKE(trim(NAME_1), '[^- '']')

但我没有得到正确的结果。
需要匹配:

Kevin#123
bob@jr
mike$dr

需要排除:

Alex-jr
Robert'jr
Brian jr
zpf6vheq

zpf6vheq1#

你需要使用 not 与要排除的值匹配的模式。否则,您将匹配包含不在排除列表中的任何字符的字符串,排除列表是所有字符。

select column_value
from   ora_mining_varchar2_nt
       ( 'Kevin#123'
       , 'bob@jr'
       , 'mike$dr'
       , 'Alex-jr'
       , 'Robert''jr'
       , 'Brian jr'
       , 'A!' )
where  1=1
and    not regexp_like(column_value,'[- '']')
and    regexp_like
       (column_value, '([A-Z0-9]+[^A-Z0-9])|([^A-Z0-9]+[A-Z0-9])', 'i') ;

编辑:添加了 regex_like 条件,以确保值包含字母或数字和“特殊字符”,这里指的是既不是字母、数字也不是空格的字符, ' 或者 - .

lnlaulya

lnlaulya2#

您可以使用以下查询来包括除 space, - and ' ```
SQL> with tbl(name) as (
2 select 'Kevin#123' from dual union
3 select 'bob@jr' from dual union
4 select 'mike$dr' from dual union
5 select 'Alex-jr' from dual union
6 select 'Brian jr' from dual union
7 select 'Brian)jr' from dual union
8 select 'Robert''jr' from dual
9 )
10 select *
11 from tbl
12 where regexp_like(name, '[^[a-z]|[A-Z]|[:space:]|[:cntrl:]|/]')
13 and not regexp_like(name,'[- '']');

NAME

Brian)jr -- see this is included
Kevin#123
bob@jr
mike$dr

SQL>

bfhwhh0e

bfhwhh0e3#

一种选择是用 NULL ,剩下的无效:

SQL> with test (col) as
  2    (select 'Robert@jr23' from dual union all
  3     select 'Kevin#123'   from dual union all
  4     select 'bob@jr'      from dual union all
  5     select 'mike$dr'     from dual union all
  6     select 'Alex-jr'     from dual union all
  7     select 'Robert''jr'  from dual union all
  8     select 'Brian jr'    from dual
  9    )
 10  select col
 11  From test
 12  where regexp_replace(col, '[[:alpha:]]|[[:digit:]]|-|''', null) is not null;

COL
-----------
Robert@jr23
Kevin#123
bob@jr
mike$dr
Brian jr

SQL>

我不知道你想得到什么结果。下面是一些例子,展示了这样一个正则表达式的结果;因此,您希望获得哪些ID?

SQL> with test (id, col) as
  2    (select 1, 'Robert@jr23' from dual union all
  3     select 2, 'Kevin#123'   from dual union all
  4     select 3, 'bob@jr'      from dual union all
  5     select 4, 'mike$dr'     from dual union all
  6     select 5, 'Alex-jr'     from dual union all
  7     select 6, 'Robert''jr'  from dual union all
  8     select 7, 'Brian jr'    from dual union all
  9     select 8, 'Brian10'     from dual
 10    )
 11  select col,
 12  regexp_replace(col,
 13    '[[:alpha:]]|[[:digit:]]|[[:space:]]|-|''', null) result
 14  from test;

COL         RESULT
----------- ----------
Robert@jr23 @
Kevin#123   #
bob@jr      @
mike$dr     $
Alex-jr
Robert'jr
Brian jr
Brian10

8 rows selected.

SQL>

你的一条评论说:
选择必须有特殊字符或数字的客户名称(空格、连字符和撇号除外)
这意味着数字和特殊字符应该被视为“相等”。如果是这样,这有帮助吗?

SQL> with test (id, col) as
  2    (select 1, 'Robert@jr23' from dual union all
  3     select 2, 'Kevin#123'   from dual union all
  4     select 3, 'bob@jr'      from dual union all
  5     select 4, 'mike$dr'     from dual union all
  6     select 5, 'Alex-jr'     from dual union all
  7     select 6, 'Robert''jr'  from dual union all
  8     select 7, 'Brian jr'    from dual union all
  9     select 8, 'Brian10'     from dual
 10    )
 11  select id, col
 12  from test
 13  where regexp_replace(col, '[[:alpha:]]|[[:space:]]|-|''', '')
 14    is not null;

        ID COL
---------- -----------
         1 Robert@jr23
         2 Kevin#123
         3 bob@jr
         4 mike$dr
         8 Brian10

SQL>

相关问题