简体   繁体   English

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

[英]How to find a row where col have alphabets,numbers or special characters (except hyphen,apostrophe and space) in Oracle SQL

I have to find the name like: Robert@jr23 (There must be Alphabet,Any Special characters or Numbers except hyphen(-),apostrophe (') and Space).我必须找到像这样的名字:Robert@jr23(必须有字母、除连字符(-)、撇号(')和空格外的任何特殊字符或数字)。

I was doing as below:我正在做如下:

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

But I am not getting the right results with this.但是我没有得到正确的结果。

Need to match:需要匹配:

Kevin#123
bob@jr
mike$dr

Needs to exclude:需要排除:

Alex-jr
Robert'jr
Brian jr

You need to use not with a pattern matching the values you want to exclude.您需要使用not与要排除的值匹配的模式。 Otherwise you are matching strings that contain any character that is not in the exclusion list, which is all of them.否则,您将匹配包含不在排除列表中的任何字符的字符串,即所有字符。

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') ;

Edit: added a regex_like condition to ensure that values contain a letter or digit and a 'special character', meaning here a character that is neither a letter, digit or space, ' or - .编辑:添加了一个regex_like条件以确保值包含字母或数字和“特殊字符”,这意味着这里的字符既不是字母、数字或空格,也不' -

One option is to replace everything that's valid with NULL , and what remains is invalid :一种选择是用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>

I'm confused about what you, actually, want to get as a result.我对你实际上想要得到的结果感到困惑。 Here are some examples which show result of such a regular expression;以下是一些显示此类正则表达式结果的示例; which IDs do you want to get as a result?作为结果,您希望获得哪些 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>

One of your comments says:你的一条评论说:

select a customer name where there must be a special character or numbers (except space, Hyphen and apostrophe) select 客户名称,其中必须有特殊字符或数字(空格、连字符和撇号除外)

which means that numbers and special characters should be treated as "equal".这意味着数字和特殊字符应该被视为“相等”。 If that's so, does this help?如果是这样,这有帮助吗?

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>

You can use following query to include all special characters except space, - and '您可以使用以下查询来包含除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>

暂无
暂无

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

相关问题 如何在 Oracle SQL 中找到 col 具有特殊字符或数字(连字符、撇号和空格除外)的行 - How to find a row where col have special characters or numbers (except hyphen,apostrophe and space) in Oracle SQL 删除Oracle字符串数据中除连字符和空格以外的特殊字符 - Remove special characters except Hyphen and Space in Oracle String Data 如何在oracle sql查询中替换除字母,数字和“连字符”之外的所有其他字符 - How to replace all other character except alphabets, number and “hyphen” in oracle sql query 正则表达式,用于限制特殊字符,数字和字母(大写除外) - Regex that restrict special characters, numbers and alphabets except UPPERCASE Oracle SQL 查询查找电话号码的特殊字符 - Oracle SQL query to find special characters for phone numbers 如何在一个oracle sql语句中删除空格、特殊字符和数字 - how to remove spaces, special characters and numbers in one oracle sql statement 如何在字符串中查找特殊字符并将其替换为SQL中的空格? - How to find a special characters in a string and replace it with a space in SQL? 从字符串中删除特殊字符和字母,db2 中 sql 查询中的数字除外 - Remove special characters and alphabets from a string except number in sql query in db2 查找除下划线和空格外包含特殊字符的行 - Find rows which contain special characters except underscore and space SQL 代码查找和抑制数字和特殊字符 - SQL code to find and suppress numbers and special characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM