[英]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.