How to Escape a Single Quote in SQL Server? – Interview Question of the Week #192

Question: How do you escape a single quote in a SQL Server string?

Answer: Write two single quotes inside the string to represent one single quote in the value.

Two matching brass clips hold a ribbon safely inside a wooden frame

A college student asked me this at a SQL user group in Bangalore. Some people laughed because it was a simple question for an experienced SQL Server professional. I did not think that was fair. We were all beginners once, and we all struggled with simple questions. Here is the example I used to answer him:

SELECT N'This is SQLAuthority''s author Pinal Dave' AS Message;

Native SSMS complete escaped apostrophe query and matching result

A doubled apostrophe becomes one apostrophe. Captured in SSMS on SQL Server 2025.

Original syntax-highlighted SQL doubles the apostrophe in SQLAuthority's
The original code capture, now accompanied by copyable SQL. It shows the query, not a result grid.

The result is This is SQLAuthority's author Pinal Dave, with one apostrophe. '' is two single-quote characters, not one double-quote character. SQL Server does not use a backslash to escape an apostrophe in an ordinary T-SQL string literal.

For application input, use parameters rather than constructing a SQL statement by repeatedly escaping user text. That is a different task from writing this literal in a script.

Related interview questions: backup compression defaults, TOP 0 versus a false WHERE, moving database files, SQLDump corruption evidence, and INSERT SELECT versus SELECT INTO.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Quotes, SQL Scripts, SQL Server
Previous Post
Where is the Default Settings for Backup Compression? – Interview Question of the Week #191
Next Post
How to STOP the Usage of SELECT * For Views? – Interview Question of the Week #193

Related Posts

1 Comment. Leave new

  • Hi Pinal,

    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.

    Thanks,
    Srini

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.