oracle like escape转义符的用法的介绍
oracle like escape转义符的用法的介绍
like 的匹配有两种模式.% and _ :
_ one charater.
% zero or more charaters.
下面是例子:
如果table_name 里面的名字里有‘_‘ or ‘%‘ 你还想引用。 就加escape 语句:
表WM$CONSTRAINTS_TABLE和表PRODUCT_DESCRIPTIONS是用来做例子的,你也可以用自己取个带有特殊字符的表名,做实验
可以先查询一下,看看结果。对比一下,就知道like + escape 语句的用处了。
例子里已经带结果了。可以看看!!!!!!!!!!!!!!!
select table_name from all_tables where TABLE_NAME like ‘W_$C%‘; ----ok
select table_name from all_tables where TABLE_NAME like ‘WM$C%‘; ----ok
select table_name from all_tables where TABLE_NAME like ‘W%$C%‘; ----ok
select table_name from all_tables where TABLE_NAME like ‘W%$%‘; ----ok
WM$CONSTRAINTS_TABLE
---------
select table_name from all_tables where TABLE_NAME like ‘P%__D%‘ ESCAPE ‘_‘; ---ok---Attention--==> two underscore ‘_‘;
select table_name from all_tables where TABLE_NAME like ‘P%_D%‘; ---ok
select table_name from all_tables where TABLE_NAME like ‘P%_D%S‘; ----ok
select table_name from all_tables where TABLE_NAME like ‘P%__D%‘ ESCAPE ‘_‘; ====>PRODUCT_DESCRIPTIONS
select table_name from all_tables where TABLE_NAME like ‘P%__R%‘ ESCAPE ‘_‘;
Note: if I don`t add the clause escape,the sytanx will give the underscore ‘_‘ two meanings.
one is the underscore,other is one character .see the oracle documents Pattern-matching conditions.
the sentence will give the resualt
===
====SQL> select table_name from all_tables where TABLE_NAME like ‘P%_D%S‘;
TABLE_NAME
------------------------------
PRODUCT_DESCRIPTIONS
PRODUCTS
========
This one is the resualt with the escape clause
===SQL> select table_name from all_tables where TABLE_NAME like ‘P%__D%‘ ESCAPE ‘_‘;
(select table_name from all_tables where TABLE_NAME like ‘P%\_D%‘ ESCAPE ‘\‘;)这句和上边的一样。怕同志们弄混了。esccape 定义的 escp_chare 后的特殊字符还原成原来的意义。
Any character can follow ESCAPE except percent (%) and underbar (_). 这句是oralce原文档里的。这里比较晕 A wildcard character is treated as a literal if preceded by the escape character.
SELECT last_name
FROM employees
WHERE last_name
LIKE ‘%A\_B%‘ ESCAPE ‘\‘;
这个是oracle 给的例子。但是我的例子里 用‘_‘同样可以得到一样的结果。
还就是在 利用索引的时候,会快一些。如果是大表的话 非常之好。但是like condition 不要用%开头,这样还是扫描原表,不走索引的。
====
TABLE_NAME
------------------------------
PROXY_DATA$
PROXY_ROLE_DATA$
PRODUCT_DESCRIPTIONS
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。