爲了更好的理解ESCAPE轉義字符,我們首先建立一個view作爲數據源
create or replace view v as
select 'ABCEDF' as vname from dual union all
select '_bcefg' from dual union all
select '_bcedf' from dual union all
select '_\bcedf' from dual union all
select '_%bcedg' from dual union all
select 'xycef' from dual;
查詢看下結果:
select * from v;
要求:查出vname中包含_ced的記錄
select * from v where vname like '_bce%'
結果多了一個abcef記錄。因爲like子句中有兩個通配符“%”“_”
這裏“_”被當做的通配符,我們需要給它轉義成字符串“_”。
下面操作:
select * from v where vname like '\_bce%' escape '\';
總結:escape '\' 起到轉義的作用。 \後面的_不再是通配符的作用了而是字符串的意思。
如果沒有oracle環境執行sql,可以試試下面的地址: