escape在什么情况下使用
2012-07-16 11:15
197 查看
举例说明:
例如我们要进行模糊查询:
--测试数据
declare @tablea table (id int ,col varchar(20))
insert into @tablea
select 1,'maco' union all
select 2,'mao' union all
select 3,'micro'
--模糊查询
select * from @tablea where col like '%ma%'
/*结果
id col
----------- --------------------
1 maco
2 mao
*/
这是最普通的模糊查询了
但是当col列含有特殊字符例如%%的时候
declare @tableb table (id int ,col varchar(20))
insert into @tableb
select 1,'m%a%co' union all
select 2,'m%a%o' union all
select 3,'miacro'
select * from @tableb where col like '%%a%%'
上面这句就不行了,结果相当于%a%,有a的都出来了
/*
结果:
id col
----------- --------------------
1 m%a%co
2 m%a%o
3 miacro
*/
此时我们可以用escape来处理
declare @tablec table (id int ,col varchar(20))
insert into @tablec
select 1,'m%a%co' union all
select 2,'m%a%o' union all
select 3,'miacro'
--模糊查询
select * from @tablec where col like '%$%a$%%' escape '$'
/*结果
id col
----------- --------------------
1 m%a%co
2 m%a%o
*/
例如我们要进行模糊查询:
--测试数据
declare @tablea table (id int ,col varchar(20))
insert into @tablea
select 1,'maco' union all
select 2,'mao' union all
select 3,'micro'
--模糊查询
select * from @tablea where col like '%ma%'
/*结果
id col
----------- --------------------
1 maco
2 mao
*/
这是最普通的模糊查询了
但是当col列含有特殊字符例如%%的时候
declare @tableb table (id int ,col varchar(20))
insert into @tableb
select 1,'m%a%co' union all
select 2,'m%a%o' union all
select 3,'miacro'
select * from @tableb where col like '%%a%%'
上面这句就不行了,结果相当于%a%,有a的都出来了
/*
结果:
id col
----------- --------------------
1 m%a%co
2 m%a%o
3 miacro
*/
此时我们可以用escape来处理
declare @tablec table (id int ,col varchar(20))
insert into @tablec
select 1,'m%a%co' union all
select 2,'m%a%o' union all
select 3,'miacro'
--模糊查询
select * from @tablec where col like '%$%a$%%' escape '$'
/*结果
id col
----------- --------------------
1 m%a%co
2 m%a%o
*/
相关文章推荐
- inline-block 在什么情况下可以使用?
- 网络监控中什么情况下使用流媒体转发服务器,有什么用?
- 什么情况下使用断言?
- 什么情况下使用hybrid,怎样配置GVRP
- 什么情况下使用hbase
- 数据结构:最小堆/哈希表/二叉树/平衡二叉树/红黑树的意义(什么情况下使用)
- iOS 闭包中的[weak self]在什么情况下需要使用,什么情况下可以不加?
- 同步和异步有何异同,在什么情况下分别使用他们?
- java中几种Map在什么情况下使用,并简单介绍原因及原理
- 简单说明什么是递归,什么情况下会使用递归,并写一个简单的递归程序。
- @responsebody一般在什么情况下使用,他的好处与坏处?
- php各种编码集详解和以及在什么情况下进行使用
- #{}和${}的区别及什么情况下必须使用${}
- [SQL] cast 与 convert 都在什么情况下使用
- [转载]什么情况下该使用变量延迟?
- DNS分别在什么情况下使用UDP和TCP?
- [导入]SQL语句在什么情况下使用全表扫描?
- 简单说明什么是递归,什么情况下会使用递归,并写一个简单的递归程序。
- 什么情况下使用Cron
- escape, encodeURI, encodeURIComponent 的使用情况