Oracle 轉義字符

一、準備特殊數據
create table t_escape(s varchar2(50));

--show define -- define "&" (hex 26)
--show escape -- escape off

set define off
set escape on
insert into t_escape values('string&text');
insert into t_escape values('string/&text');
insert into t_escape values('string/%text');
insert into t_escape values('string/_text');
insert into t_escape values('string''text');
set escape off
set define on

insert into t_escape values('Top_Gun');
insert into t_escape values('Tom''s eye');
insert into t_escape values('normal');
insert into t_escape values('100%ps');
insert into t_escape values('12345');
insert into t_escape values('15%30');
insert into t_escape values('%40');
insert into t_escape values('25%XT');
insert into t_escape values('%%A3E5');
set define off
--直接插入如下數據時,插入結果不正確。
insert into t_escape values('T&T Company');
set define on
commit;
/

二、使用轉義查詢

[界定符][轉義符]%'escape'[界定符] 爲轉義定義,如
轉義_,使用$_%'escape'$,這裏界定符是$。

select * from t_escape where s like 'Top$_%'escape'$';
界定符可以是空格,/,=,/,|,不能是字母。

例,查找含有'%'的所有字段:
select * from t_escape where s like '%/%%' escape '/';

'&'不能通過轉義字符查找:
select * from t_escape where s like '%/&%' escape'/';
可以通過另外的方式進行轉義:
select ascii('&') from dual; -- ASCII = 38
select * from t_escape where s like '%'||chr(38)||'%';

查找單引號:
select * from t_escape where s like '%''%';

發佈了44 篇原創文章 · 獲贊 5 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章