在LIKE中转义通配符

The*_*ler 1 sql oracle sql-like

在Oracle中使用SQL 运算符时,如何转义通配符(_%)LIKE

我今天遇到了一个愚蠢的问题.我需要_使用在varchar列上搜索下划线的存在LIKE.它不起作用 - 正如预期的那样 - 因为下划线是根据SQL的通配符.这是我的(简化)代码:

create table property (
  name varchar(20),
  value varchar(50)
);

insert into property (name, value) values ('port', '8120');
insert into property (name, value) values ('max_width', '90');
insert into property (name, value) values ('taxrate%', '5.20');
Run Code Online (Sandbox Code Playgroud)

我在PostgreSQL中尝试了以下查询,它们返回我想要的行:

select * from property where name like '%\_%'; -- should return: max_width

select * from property where name like '%\%%'; -- should return: taxrate%
Run Code Online (Sandbox Code Playgroud)

不幸的是,它在Oracle 12c中不起作用.是否存在逃避通配符的"标准"方式?或者至少在Oracle中有效的东西?

Ale*_*ole 5

您可以使用escape语法

您可以通过使用标识转义字符的子句包含实际字符%_模式ESCAPE.如果转义字符位于字符之前%_模式中,则Oracle会在模式中按字面解释此字符,而不是作为特殊的模式匹配字符.

所以你可以这样做:

select * from property where name like '%\_%' escape '\';

NAME                 VALUE                                             
-------------------- --------------------------------------------------
max_width            90                                                

select * from property where name like '%\%%' escape '\';

NAME                 VALUE                                             
-------------------- --------------------------------------------------
taxrate%             5.20                                              
Run Code Online (Sandbox Code Playgroud)