查询以选择包含特殊字符的值

时间:2018-04-23 04:11:01

标签: sql database oracle

我正在尝试在下面的查询中从列街获取带有特殊字符的记录:

select A.Street  
from ADRC a
where A.Street  LIKE '%[^A-Za-z0-9, ]%';

但它不会返回结果。我想选择包含特殊(非字母数字)字符的任何记录:

enter image description here

我刚用'*'检查过,你可以看到有很多特殊字符的数据。

1 个答案:

答案 0 :(得分:-3)

SQL Server版本:

你不需要%[^] 因为喜欢这样做

select A.Street  
from ADRC a
where A.Street  LIKE '[A-Za-z0-9, ]%';

结果:

Oracle版本:

WITH temp_table AS (
    SELECT '*royal oak' Street from dual
    UNION ALL
    SELECT '459*' Street from dual
    UNION ALL
    SELECT '5TH* AVE' Street from dual
    UNION ALL
    SELECT ' 5TH* AVE' Street from dual    
    UNION ALL
    SELECT ',5TH* AVE' Street from dual        
)
select A.Street  
from temp_table a
where REGEXP_LIKE(A.Street,'^[A-Za-z0-9, ]') ;

新问题:

  

如何在所有5列中获取包含spcl字符的记录?

WITH temp_table AS (
    SELECT '*royal oak' col1,'*royal oak' col2,'*royal oak' col3,'*royal oak' col4,'*royal oak' col5 from dual
    UNION ALL
    SELECT '5TH* AVE' col1,'5TH* AVE' col2,'5TH* AVE' col3,'5TH* AVE' col4,'5TH* AVE' col5 from dual

)
select *  
from temp_table a
where REGEXP_LIKE(A.col1,'^[A-Za-z0-9, ]') or REGEXP_LIKE(A.col2,'^[A-Za-z0-9, ]')
or REGEXP_LIKE(A.col3,'^[A-Za-z0-9, ]') or REGEXP_LIKE(A.col4,'^[A-Za-z0-9, ]') or REGEXP_LIKE(A.col5,'^[A-Za-z0-9, ]');