How to Insert a Single Quote in SQL

The easiest way to insert a single quote into a SQL query is to use the ESCAPE keyword. This keyword allows you to specify a character that will be treated as a literal character, rather than a special character. For example, if you wanted to insert a single quote into a query, you would use the following syntax:

SELECT * FROM table_name WHERE column_name = 'value\' ESCAPE '\';

The ESCAPE keyword tells the database to treat the single quote as a literal character, rather than a special character. This ensures that the query is properly formatted and will execute correctly.

Examples

Let's look at a few examples of how this technique can be used. Suppose you have a table called users with the following data:

If you wanted to select all users whose name is John's, you would use the following query:

SELECT * FROM users WHERE name = 'John\'s' ESCAPE '\';

This query would return all users whose name is John's, since the single quote is treated as a literal character.

Additional Info

The ESCAPE keyword is supported by most major databases, including MySQL, PostgreSQL, and SQL Server. However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database. 🤓

Want to build your own LLM Apps with AirOps👇👇

users
nameage
John25
Jane30
John25
Bob20