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:
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:
After we run that INSERT statement, we see the value in the table contains just one single quote:
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:
I’ll use this same WHERE clause to delete the row from the table:
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!