How do I escape a single quote in SQL Server?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Escaping a single quote in SQL Server is a crucial technique to understand, especially when you are working with string data that might contain single quotes. This is often needed to ensure that SQL queries execute correctly without errors and to help guard against SQL injection vulnerabilities. In this article, we delve into why and how to escape single quotes, along with providing specific examples and a key points table for quick reference.
Understanding the Need to Escape Single Quotes
In SQL Server, the single quote character (') is used as the delimiter for string literals. When SQL Server encounters a single quote within a string, it interprets it as the end of the string. If your string contains a single quote (e.g., O'Reilly), you need to escape the single quote to inform SQL Server that it should treat it as a literal character rather than a string delimiter.
Techniques to Escape a Single Quote
Doubling Up Single Quotes
The most common way to escape a single quote in SQL Server is by doubling it. When SQL Server sees two single quotes, it treats them as a literal single quote within the string.
Example:
This SQL statement treats O''Reilly as O'Reilly in the output.
Using Stored Procedures or Parameterized Queries
Another robust method, especially important from a security perspective, is to use parameterized queries or stored procedures. These techniques avoid the need to manually escape single quotes, thus reducing the risk of SQL injection.
Example using a parameterized query:
Example using a stored procedure:
Then execute the stored procedure:
Why Escaping Properly is Important
- Syntax Correctness: To ensure your SQL commands do not fail due to syntax errors caused by misplaced single quotes.
- Security: To prevent SQL injection, a common attack where an attacker can execute arbitrary SQL code.
Summary Table: Techniques to Escape Single Quotes in SQL Server
| Method | Usage Scenario | Example |
| Doubling single quotes | Quick text manipulations | SELECT 'O''Reilly' AS Publisher; |
| Parameterized queries | Application development | SET @PublisherName = 'O''Reilly'; |
| Using stored procedures with parameters | Application development | EXEC FindPublisher @PublisherName = 'O''Reilly'; |
Additional Considerations
- Testing: Always test your SQL queries to ensure that escaping is done correctly, particularly when it could affect the logical outcomes of your scripts.
- Use Tools and Libraries: When developing applications, prefer using database tools, ORMs, or frameworks that automatically handle escaping and safeguard against SQL injection.
Conclusion
Understanding how to properly escape a single quote in SQL Server is essential for both functioning and secure SQL scripting. Whether you're generating reports, writing applications that connect to a SQL Server database, or performing ad-hoc database manipulation, knowing how to handle single quotes correctly is an indispensable skill.
Related reading
- How do I escape reserved words used as column names? MySQL/Create Table
- How do I extract the created date out of a Mongo ObjectID
- How do I filter query objects by date range in Django?
- How do I find out my MySQL URL, host, port and username?
- How do I find out my MySQL URL, host, port and username?
- How do I find the MySQL my.cnf location
- How do I find which transaction is causing a Waiting for table metadata lock state?
- How do I get a list of column names from a psycopg2 cursor?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.