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.

I hope this post has made your work a bit more easier than before. Your suggestions and comments are always welcome. :)

Tags: , ,

13 comments

  1. It really helped me.

  2. Hey… I’m glad it did :)

  3. Thank you! You have no idea how much trouble you saved me.

    Who at MS thought this was a good idea? “The rest of the world uses a slash, so we should definitely do something dumb, instead.”

  4. Hey thanks a lot for the encouraging comment on your part James. Motivates me to blog more :)

  5. Thx very much

  6. Just for completeness =)

    Have you ever think you’ll need to search-like for square brace itself? Some ‘-![CoolName]!-’ for example?

    The solution is a bit tricky – you need to escape only opening [ square.
    select * from table where column like '%[[]%’

    other workable expressions – like ‘%]%’, ‘%[[]%]%’

    Not very obvious, but still logical ;)

    (way 2 is possible here too, but how many ORMs support such syntax?)

  7. @Kam99

    Thanks for sharing this info here. Thats a bit tricky alright :)

    About the second way of using ESCAPE.. I’m not too sure if its supported in other databases.

  8. Thank’s yaar,
    it increses few more drop in my gyan sagar…. :)

  9. @Ashok

    you’re welcome buddy ;)

  10. Thank you !
    Very helful, especially since informations on the way to handle MS SQL via PHP are not plentiful/easy to find on the web.

  11. Thanks …. exact what i needed in this moment *G

  12. Hello – I am looking for a way in SQL to use wildcards to search all names with a tick. example O’Gorman. (Like ‘%’%') and a few other variations (Like ‘%”‘”%’) are unsucessful. Any suggestions?
    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>