繁体   English   中英

SQL Only字符串搜索

[英]Sql Only String search

with temp(name) as (select 'abc' from dual
union select '123abc' from dual
union select '1abc3' from dual)
select * from temp where temp.name  like '%[a-z]%'

为什么我无法在输出中获得所有3条记录,我正在使用Oracle SQL Developer 11g

我使用此查询的原因是:我想检查所需的列是否仅包含字符串,没有数字和特殊字符

您正在混合使用SQL Server和Oracle语法:

SQL SERVER演示

with temp(name) as (
      select 'abc' union all select '123abc'  union select '1abc3' 
)
select * from temp where temp.name like '%[a-z]%';
                                        -- [] is T-SQL specific

ORACLE演示

with temp(name) as (select 'abc' from dual
union select '123abc' from dual
union select '1abc3' from dual)
select * from temp where regexp_like(temp.name, '[a-z]')

首先,最好使用union all而不是union ,因为union会导致除去重复项的开销。

其次,您的like模式是非标准的,仅在SQL Server和Sybase中受支持。 换句话说,您的名字均不包含方括号或连字符,因此均不匹配该模式。

这符合您的期望吗?

with temp(name) as (
      select 'abc' from dual union all
      select '123abc' from dual union all
      select '1abc3' from dual
     )
select *
from temp
where temp.name like '%a%';

这只是检查语法。 它不等同于您的like模式。

我从语法上推测您正在使用Oracle。 如果是这样,则可以使用正则表达式:

where regexp_like(temp.name, '[a-z]')

大多数(但不是全部)数据库都支持正则表达式,因此您可以在大多数数据库中表达类似的逻辑。

首先,您正在混合概念。 您试图通过使用普通的LIKE运算符进行正则表达式比较。 其次,您应该使用您正在使用的数据库标记问题。 考虑到dual表,我假设您正在使用oracle

WITH temp(name) AS (SELECT 'abc' FROM dual
    UNION SELECT '123abc' FROM dual
    UNION SELECT '1abc3' FROM dual)
SELECT * FROM temp 
WHERE REGEXP_LIKE(temp.name,'[a-z]+')

另请注意,使用%通配符将无法正常使用正则表达式匹配。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM