回答

收藏

在LIKE中转义通配符

技术问答 技术问答 79 人阅读 | 0 人回复 | 2023-09-13

在Oracle中使用SQL如何转换通配符?(_和%)LIKE?$ V$ y# x: V5 ?5 M$ |2 H
我今天遇到了一个愚蠢的问题。我需要用它来搜索_varchar上面是否有下划线LIKE。正如预期的那样,它不起作用-7 s; o, y, H( @* M! c4 b1 @) P( }
因为根据SQL,下划线是通配符。这是我的(简化)代码:
6 j/ ?  K2 _" ~0 Rcreate 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');我在PostgreSQL尝试了以下查询,他们回到了我想要的行:+ j& L! y2 F3 w2 p: b- I
select * from property where name like '%\_%'; -- should return: max_widthselect * from property where name like '%\%%'; -- should return: taxrate%不幸的是,它在Oracle 12c不起作用。有没有标准的转义通配符?或者至少在Oracle中有效?4 m' l$ ?' T3 g" U! h7 Q1 N
                                                               
, `1 F: @2 |# y* x; p8 f& \: g    解决方案:                                                                4 z+ s" S5 O) H) o% [: o7 [
                                                                你能用的escape语法
( @( U: T( R" t$ o您可以使用包含实际字符%或%的子句_在模式中,该ESCAPE如果转义字符在字符%或_在模型之前,则Oracle该字符将在模式中以字面意义解释,而不是将其解释为特殊模式。* S3 N) ?& K; M9 J/ L/ U' ?0 e
因此,您可以执行以下操作:) V, j, {. R# U: e% X7 O  C
select * from property where name like '%\_%' escape '\';NAME                 VALUE                                             -------------------- --------------------------------------------------max_width      select * from property where name like '%\%%' escape '\';NAME                 VALUE                                             -------------------- --------------------------------------------------taxrate           5555555                   .2020
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则