How to Escape a Single Quote in SQL ?
Last Updated :
19 Dec, 2024
Structured Query Language (SQL) is an essential tool for manipulating relational databases in the growing field of data management. Whether we are a software developer, a database administrator, or an aspiring data analyst, knowing SQL is critical for accurate and effective data manipulation and retrieval. While SQL is one of the most effective tools for maintaining and working with relational databases.
However, one common problem when working with SQL is having to escape specific characters, such as single quotes. In this article, we will explain the significance of escaping single quotes in SQL, potential drawbacks, and workable solutions for handling these scenarios.
How to Escape Single Quotes in SQL
Single quotes are used in SQL to separate string literals. It is used to represent text values in SQL queries and allows developers to work with alphanumeric data. However, if the data itself contains single quotes, it can cause syntax errors or even lead to potential security issues, such as SQL injection attacks. To address these issues, escaping single quotes is necessary for maintaining proper syntax and database security.
This article discusses two common methods for escaping single quotes in SQL:
- Using Double Single Quotes('')
- Using CHAR Function
Setting Up the Environment
Let’s create a sample table and populate it with some values for demonstration purposes. This setup will provide a clear understanding of how to handle single quotes in SQL. It will also help in testing and validating the queries effectively.
Creating the Table
CREATE TABLE customer (
id INT,
first_name VARCHAR(20),
last_name VARCHAR(20)
);
INSERT INTO customers VALUES
('1', 'John', 'Doe'),
('2', 'Jane', 'Smith'),
('3', 'Bob', 'Johnson');
SELECT * FROM customers;
Output

1. Using Double Single Quotes('')
One of the simplest ways to escape single quotes in SQL is by doubling them. This method is straightforward and commonly used for escaping quotes in string literals. Correct grammar and precise data retrieval are made possible by treating the two consecutive single quotes as an escaped single quotation within the string.
Query:
-- Update the last name of the customer with id 2
UPDATE customers
SET last_name = 'O''Neill'
WHERE id = 2;
Output

Explanation:
- The single quote in
O'Neill
is escaped by doubling it (O''Neill
).
- This ensures the SQL parser treats it as part of the string rather than a syntax delimiter.
- The
last_name
of the customer with ID 2 is updated to O'Neill
.
2. Using CHAR Function
Another approach to escape single quotes is by using the ASCII code for the single quote (39) with the CHAR
function. This method is particularly useful for building dynamic SQL queries in stored procedures. The single quotation character is represented by the CHAR(39) function, which offers a simple and direct method of escaping it inside the string.
Query:
-- Update the last name of the customer with id 2 using CHAR function
UPDATE customers
SET last_name = 'O' || CHAR(39) || 'Neill'
WHERE id = 3;
Output

Explanation:
- The
CHAR(39)
function represents the ASCII code for a single quote.
- The query concatenates the string
'O'
, the single quote (CHAR(39)
), and the string 'Neill'
to form O'Neill
.
- The
last_name
of the customer with ID 3 is updated to O'Neill
Important Points to Handle Single Quotes in SQL
- Always test your queries on a non-production environment before applying changes to our database.
- Use parameterized queries or prepared statements to avoid SQL injection attacks.
- Choose the method that best suits our use case (e.g., double single quotes for simplicity or
CHAR
for dynamic queries).
Conclusion
To maintain data integrity and avoid syntax errors, single quotes in SQL must be escaped from the table. Effective database management requires that we know how to handle single quotes in our SQL queries, whether we use the CHAR function or the double single quote method. Through the application of these strategies, we will be able to handle the subtleties of SQL syntax, reduce possible security threats, and preserve the stability of our database functions.