PostgreSQL – How to Escape an apostrophe(single quote) within a String
You can escape a single quote (also known as an apostrophe) within a string by using another single quote. This is a common practice in SQL to handle single quotes within string literals. Here’s how you can do it:
To represent a single quote within a string, you can double the single quote character (''
). Here’s an example:
INSERT INTO my_table (column_name) VALUES ('It''s a nice day');
In the example above, the single quote within the string is escaped by doubling it (''
). When this string is inserted into the my_table
table, it will be stored as “It’s a nice day”).
You can use this technique in SQL statements wherever you need to include single quotes within string literals, such as in INSERT
, UPDATE
, or SELECT
statements.