SQL Server_escape函数使用
--SQL Server中escape的使用方法:
create table tabname ( id int , col varchar(20) ) insert into tabname select 1,'Hello' union all select 2,'Hell' union all select 3,'llo'
--模糊查询
select * from tabname where col like '%He%'
--结果:
------------------------------------------------------------
1 Hello 2 Hell
------------------------------------------------------------
--当col列含有特殊字符例如%时:
insert into tabname select 4,'H%e%llo' union all select 5,'H%e%ll' union all select 6,'lleo'
--模糊查询
select * from tabname where col like '%%e%%'
--结果:
------------------------------
1 Hello 2 Hell 4 H%e%llo 5 H%e%ll 6 lleo
------------------------------
--注:我们想的得到的结果是:
------------------------------------------------------------
1 H%e%llo 2 H%e%ll
------------------------------------------------------------
--此时可以用escape解决:
select * from tabname where col like '%#%e#%%' escape '#'
--结果:
------------------------------
1 H%e%llo 2 H%e%ll
------------------------------