Escape single quotes and wild cards ‘%’ , ‘_’ in MS SQL

 

To escape single quotes ( ‘ ) in MS SQL all you need to do is add two single quotes instead of one.

Ex. If you want to insert the name D’souza into MS SQL the query goes like this:

INSERT into name_column ([name]) VALUES (‘D”Souza’)

Look carefully. We have replaced single quote in between the letter D and the word SOUZA with two SINGLE QUOTES. (It’s not a double quote)

 

 

Similarly, you may also face a problem while querying for values which contain the characters ‘%’ and ‘_’ in them. This is so because SQL considers these two characters as wild cards for multiple characters and single character string matching.

There are 2 ways to escape these wild cards.

1. You can use the default square braces “[]” like this- [%] or [_]

2. You can define your own escape character by using the keyword ESCAPE at the end of your where clause.

Ex. select * from name_column where name like ‘gyan\_sagar’ ESCAPE ‘\’

In the example above we escape “_” by defining “\” as an escape character using the ESCAPE keyword.

Note: You can only define a single character for escaping and not a string.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值