How to escape a single quote in SQL Server

sql server escape single quote featured image
Reading Time: 2 minutes

SQL Server makes it very easy to escape a single quote when querying, inserting, updating or deleting data in a database.



Here’s how it’s done:

Just use another single quote

For example, let’s take a look at a table called Books:

sql server escape single quote Books table

Let’s say we want to insert another row for the book Debt Free Degree written by Anthony O’Neil.

If we want to escape the single quote in the name O’Neil, we need to escape it with another single quote. Like this:

sql server escape single quote insert statement 1

After we run that INSERT statement, we see the value in the table contains just one single quote:

sql server escape single quote select statement

If we want to write a WHERE clause looking for the value O’Neil, we would also escape the single quote by using another single quote:

sql server escape single quote where clause 1

I’ll use this same WHERE clause to delete the row from the table:

SQL SERVER escape single quote where clause

It’s as easy as that!



Next Steps:

Leave a comment if you enjoyed this micro-tutorial on how to escape a single quote in SQL Server.

If you are just starting out with SQL Server, you would probably benefit from the list of tutorials I have in the Simple SQL Basics category I have created. All the tutorials in this category will help you understand the absolute fundamentals you need to know when querying and developing SQL Server databases. Make sure you check it out!:

Simple SQL Tutorials: The Basics

Thank you very much for reading!



Make sure you subscribe to my newsletter to receive special offers and notifications anytime a new tutorial is released!

If you have any questions, leave a comment. Or better yet, send me an email!

Related Post

Leave a Reply

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