电竞比分网-中国电竞赛事及体育赛事平台

分享

Oracle中使用escape關(guān)鍵字實現(xiàn)like匹配特殊字符,以及&字符的轉(zhuǎn)義

 liuyang_inf 2014-03-20
問題描述:如果在一個表中的一個字段上存在'&',  '_',  '%'這樣的特殊字符,而我們又得在where條件中使用到這些特殊字符怎么辦?


1.創(chuàng)建含有這些特殊字符的表 

SQL> create table t_test_escape(name varchar2(20));
 
Table created
 
SQL> insert into t_test_escape(name) values('&_hello');
 
1 row inserted

--這里會提示我輸入變量_hello的值,我沒有輸入任何值,所以為空!
 
SQL> select * from t_test_escape;
 
NAME
--------------------

結(jié)果自然也為空
 
SQL> truncate table t_test_escape;
 
Table truncated

--使用set define off 關(guān)閉替代變量的功能
SQL> set define off
SQL> insert into t_test_escape(name) values('&_hello');
 
1 row inserted
 
SQL> insert into t_test_escape(name) values('%%_hello');
 
1 row inserted
 
SQL> insert into t_test_escape(name) values('Oracle%&_hello');
 
1 row inserted
 
SQL> commit;
 
Commit complete
 
SQL> select * from t_test_escape;
 
NAME
--------------------
&_hello
%%_hello
Oracle%&_hello

--使用set define off關(guān)閉變量替換功能之后,果然能夠插入含有&的特殊字符了。


2.使用escape關(guān)鍵字在模糊查詢中查看字符中含有%的字符串 
SQL> select * from t_test_escape where name like '%a%%' escape 'a';
 
NAME
--------------------
%%_hello
Oracle%&_hello

--上面使用的轉(zhuǎn)義字符為'a'

3.使用escape關(guān)鍵字模糊查詢含有'&'的字符串
因為此時還是set define off的 所以這個時候&并不是什么特殊字符,所以下面的查詢會報錯
SQL> select * from t_test_escape where name like '%a&%' escape 'a';
 
select * from t_test_escape where name like '%a&%' escape 'a'
 
ORA-01424: missing or illegal character following the escape character
 
SQL> insert into t_test_escape(name) values('Oracle%&hello');
 
1 row inserted
 
SQL> commit;
 
Commit complete
 
SQL> select * from t_test_escape where name like '%a&h%' escape 'a';
 
select * from t_test_escape where name like '%a&h%' escape 'a'
 
ORA-01424: missing or illegal character following the escape character


在set define off關(guān)閉替代變量功能之后可以直接將&當做普通字符,而不用escape
SQL> select * from t_test_escape where name like '%&h%';
 
NAME
--------------------
Oracle%&hello
 
使用set define on打開替代變量功能
SQL> set define on;
SQL> select * from t_test_escape where name like '%&h%';

--這里會提示我輸入變量h的值,因為我沒有輸入任何值,這條sql條件相當于就是like '%%',所以返回全部數(shù)據(jù)
 
NAME
--------------------
&_hello
%%_hello
Oracle%&_hello
Oracle%&hello
 
--使用escape關(guān)鍵字指定特定的轉(zhuǎn)義字符試試看
SQL> select * from t_test_escape where name like '%a&h%' escape 'a';

 
NAME
--------------------

還是會提示我輸入變量h的值

--下面通過查詢出'&'的ascii來繞過這個障礙
SQL> select ascii('&') from dual;
 
ASCII('&')
----------
        38

--使用chr(38)去替代特殊字符'&'
SQL> select * from t_test_escape where name like '%' || chr(38) || 'h%';
 
NAME
--------------------
Oracle%&hello
 

4.使用escape關(guān)鍵字模糊查詢含有'&'的字符串
SQL> select * from t_test_escape where name like '%a_%' escape 'a';
 
NAME
--------------------
&_hello
%%_hello
Oracle%&_hello

5.下面我將替代變量的特殊字符改為$試試,然后看能不能使用模糊匹配匹配特殊字符'&'
SQL> set define $
SQL> select * from t_test_escape where name like '%&h%';
 
NAME
--------------------
Oracle%&hello


總結(jié):對于使用escape關(guān)鍵字去轉(zhuǎn)義特殊字符的時候,并不是對于所有的特殊字符都能夠轉(zhuǎn)義成功,上面的實驗表明,對于'%', '_', '&'來說,使用escape是能夠成功轉(zhuǎn)義'%', '_'的,但是卻不能轉(zhuǎn)義'&',當然這只是默認的情況,如果我們將綁定變量的標識符設(shè)置為非默認的$,那么我們就可以把'&'當做普通字符對待了。如果不改變綁定變量的默認標識符,那么就使用chr(38)去替代'&'!





    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多