加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

escape在sqlserver中的使用--【叶子】

发布时间:2020-12-12 15:34:14 所属栏目:MsSql教程 来源:网络整理
导读:escape 在 sqlserver 中的使用 -- 【叶子】 ? 1 、 escape 是干什么用的? escape 相对于转移字符 ? 2 、 escape 在什么情况下使用? 举例说明: 例如我们要进行模糊查询: ? -- 测试数据 declare @tablea table ( id int , col varchar ( 20 )) insert into

escapesqlserver中的使用--【叶子】

?

1escape是干什么用的?

escape相对于转移字符

?

2escape在什么情况下使用?

举例说明:

例如我们要进行模糊查询:

?

--测试数据

declare @tablea table (id int ,col varchar(20))

insert into @tablea

select 1,'maco' union all

2,'mao' union all

3,'micro'

--模糊查询

* from @tablea where col like '%ma%'

/*结果

id????????? col

----------- --------------------

1?????????? maco

2?????????? mao

*/

?

这是最普通的模糊查询了

但是当col列含有特殊字符例如%%的时候

?

@tableb table (id int ,"serif"; mso-no-proof: yes;'> into @tableb

union all

union all

* from @tableb where col like '%%a%%'

?

上面这句就不行了,结果相当于%a%,a的都出来了

/*

结果:

1?????????? m%a%co

2?????????? m%a%o

3?????????? miacro

?

此时我们可以用escape来处理

@tablec table (id int ,"serif"; mso-no-proof: yes;'> into @tablec

* from @tablec where col like '%$%a$%%' escape '$'

2?????????? m%a%o

*/

@【叶子】http://blog.csdn.net/maco_wang 原创作品,转贴请注明作者和出处,留此信息。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读