We have a search screen that allows users to search for a party name. Is there any way to get the search screen to accept the character ' as a character in the search screen without messing up the SQL query?
As you are noticing, the single quote is a termination character for a string literal in the SQL language. The following query works just fine:
SELECT * FROM emp WHERE name='Smith';But the following query does not work fine:
SELECT * FROM emp WHERE name='O'Shea';There is a mismatched number of single quotes. Luckily, you can escape the middle quote, which you need for your literal. You can do this as follows:
SELECT * FROM emp WHERE name='O''Shea';Notice that I've used two single quotes (not one double quote) in the middle of the string.
For More Information
- Dozens more answers to tough Oracle questions from Brian Peasland are available.
- The Best Oracle Web Links: tips, tutorials, scripts, and more.
- Have an Oracle or SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical Oracle and SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
Dig Deeper on Oracle database design and architecture
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.