Question: How to Escape a Single Quote in SQL Server?
Answer: Yesterday, I had an opportunity to visit a SQL User Group in Bangalore, India, and during the event, I had received a very simple question from a college student. As soon as he asked the question, everyone laughed at this question as the question was relatively simple if you are a SQL Server professional.
At one point in time, we were all beginners and we all struggled with the simple questions too. I really felt bad when everyone laughed at the newbie. I decided to answer him during the session and here is the simple possible way to escape a single quote in SQL Server string.
You can escape a single quote with another single quote. Here is the example –
Please note that you can’t escape with a double quote but with double single quotes.
Here are few of the previous blog posts related to Interview Questions and Answers:
- Where is the Default Settings for Backup Compression?
- Which is Optimal – TOP 0 OR WHERE 1 = 2?
- How to Move SQL Server MDF and LDF Files?
- What Part of the SQLDump Suggest Database Corruption?
- Which is Faster – INSERT INTO SELECT or SELECT INTO?
Reference: Pinal Dave (http://blog.SQLAuthority.com)
I agree with you, even I feel bad. At least, the student wants to clear his doubt and build the core concepts. May be most of the one’s who laughed at him even might not be knowing how to escape single quote. As per my view, it is good to ask even basics questions and make your concepts crystal clear rather than feeling shy of asking.