How to Insert Text With Single Quotes in PostgreSQL
Last Updated :
08 Feb, 2024
SQL stands for structured query language and is used to query databases for analytical needs. While using arithmetic queries, some results require strings to help them understand better. Strings can be formed by enclosing text in quotes. However in a case when quotes are themselves required in a string, then we need to escape them. In this article, we are going to see how we can escape a single quote in PostgreSQL.
Single Quote in PostgreSQL
Single quote (') is a special character used to denote the beginning and end of a string literal. It is used to represent text values in SQL queries and allows developers to work with alphanumeric data. The single quote serves as a delimiter for allowsvalues. When a sequence of characters is enclosed within single quotes, PostgreSQL interprets it as a string literal. When a string contains special characters or reserved keywords, using single quotes helps PostgreSQL understand that the content between the quotes is a literal string and not a part of the SQL syntax.
Setting Up Environment
Let us start by creating a table and insert some values in it. Suppose we have the following customer table. The following query creates the customer table and inserts a few records in it.
-- Create the customer table
CREATE TABLE customer (
id INT,
name VARCHAR(20)
);
-- Insert sample values into the customer table
INSERT INTO customer VALUES
('1', 'John Doe'),
('2', 'Jane Smith'),
('3', 'Bob Johnson');
The following is the initial data in the table:
Initial dataUsing Quotes in PostgreSQL
In PostgreSQL, quotes are used to denote strings. For instance, the following query returns a string:
Query:
--Example: Using quotes to create a string
SELECT ‘String value’
Output:
String value output
Explanation: When using quotes, it's important to note that column names should not be enclosed in quotes, as it results in a constant value in each row:
Quotes select the text in the form of a string. We cannot enclose column names in quotes as it will result in a string with that value instead. See the following example.
Query:
-- Display the contents of the customer table with a constant string
SELECT Name, 'Name' FROM customer;
Output:
query output
Explanation: As we can see the column name when enclosed just returns a constant value in each row.
How to Escape Single Quotes in PostgreSQL
Sometimes we might need to put quotes inside the text. We can do it in the following two ways.
Method 1: Use CHR() Function
We can make use of CHR() function to escape single quote. Single quote is represented by CHR(39) in PostgreSQL.
Query:
SELECT chr(39) || 'Name' || chr(39) FROM customer;
Output:
Using chr() Function
Explanation: Thus making use of chr() function helps to escape single quotes.
Method 2: Write Them Twice Consecutively
In some cases, when you cannot use double quotes, you can escape single quotes inside single quotes by writing them along with each other. Every single quote escapes the next one (except the border ones).
Query:
SELECT '''Name' FROM customer;
Output:
query output
Explanation: As you can see, we enclosed single quotes inside single quotes by writing them consecutively.
Inserting Text with Single Quote in PostgreSQL
Let us start by creating a table which contains information of the employees. The following query creates the table:
CREATE TABLE description (
info VARCHAR(50)
);
At present the table is empty. Let us now insert some records in the table. We will deliberately insert records in the table which has single quotes. We are going to make use of both the methods that we went through previously in this article to escape the single quote while inserting the data. The following query inserts three records in the table:
INSERT INTO description VALUES
('John Doe''s id is 1'),
('Jane Smith' || chr(39) || 's id is 2'),
('Bob Johnson''s id is 3');
As you can see we used the second method while inserting the first and the third records while used the first method to insert the second record in the table. The following is the data in the table after executing the above query.
Table dataConclusion
After reading whole article now you have good understanding of how to escape single quote and insert them in PostgreSQL. We saw two methods to escape single quotes, the first using CHR() function and then later writing them twice consecutively with their examples. Now you can easily insert data with single quotes to tables in PostgreSQL.
Similar Reads
How to Insert Multiple Rows to a Table in PostgreSQL? Inserting multiple rows into a table in PostgreSQL is a common and efficient operation, especially when handling large datasets. By executing a single SQL query, multiple rows can be added simultaneously, reducing overhead and enhancing performance. This method is particularly valuable in scenarios
5 min read
How to Insert a Line Break in a String PostgreSQL ? In PostgreSQL, managing strings efficiently is crucial, especially when dealing with VARCHAR and NVARCHAR data types. One common challenge developers face is inserting line breaks within these strings. In this article, we'll explore different approaches to tackle this issue, allowing us to format ou
5 min read
How to Create a New User With Full Privileges in PostgreSQL? PostgreSQL provides a way to give a user full privileges to do anything with the database. The database objects like schema, table, function, and so on. The 'GRANT' command is used in PostgreSQL to provide a user with any specific privileges or to override the role of the user. In this article, we a
6 min read
PostgreSQL - Dollar-Quoted String Constants When working with PostgreSQL, managing string literals can sometimes be complex, especially when dealing with special characters like single quotes ('), backslashes (), or double quotes ("). Dollar-quoting, introduced in PostgreSQL 8.0, simplifies this process, offering an elegant way to handle stri
4 min read
How to Insert a Line Break in a SQLite VARCHAR String SQLite is a popular relational database management system known for its simplicity and flexibility. However, inserting a line break in a VARCHAR/NVARCHAR string can be challenging due to SQLite's limited support for special characters. In this article, we will explore different approaches to inserti
4 min read
PostgreSQL - Insert Multiple Values in Various Rows PostgreSQL, one of the most popular relational database management systems (RDBMS), is widely used for storing structured data in a tabular format, much like MySQL. In relational databases, data is stored in tables where each row represents a record and each column represents an attribute. One of th
3 min read