How do you properly escape single quotes inside string literals in SQL Server? | A1Lab@HumairaM to SQLEnglish • 2 days ago

How do you properly escape single quotes inside string literals in SQL Server?

message-square
0

In SQL Server (T-SQL), single quotes (') delimit string literals, so to include a literal single quote inside a string, double the single quote ('')—never use a backslash (\'), which SQL Server treats as a literal backslash followed by a closing quote. For Unicode strings, use the N prefix along with doubled single quotes (N'It''s a test').


1. Basic Escaping with Doubled Single Quotes ('')

Place two single quotes together wherever you want a single quote character to appear inside the string value.

-- Inserts: O'Reilly Media
INSERT INTO dbo.publishers (publisher_name)
VALUES ('O''Reilly Media');

-- Filters rows where customer_name equals: D'Angelo
SELECT
    customer_id,
    customer_name
FROM dbo.customers
WHERE customer_name = 'D''Angelo';
  • Two consecutive single quotes inside '...' evaluate to one literal single quote character in the stored or returned string.
  • Backslashes (\) do not escape quotes in T-SQL: 'It\'s' causes a syntax error near s.

2. Unicode Strings (N'...') and Multiple Quotes

When working with NVARCHAR / NCHAR data or multiple quotes in a single string (such as contractions, apostrophes, or SQL inside strings), double every single quote inside the N'...' literal:

-- Inserts: 'Ali's' book is ready
INSERT INTO dbo.notes (note_text)
VALUES (N'''Ali''s'' book is ready');
  • Leading '' produces an opening single quote ' in the text.
  • Ali''s produces Ali's.
  • Trailing '' produces a closing single quote ' in the text.

3. Escaping Inside Dynamic SQL (EXEC / sp_executesql)

Dynamic SQL often trips up developers because strings are parsed twice: once in the outer query and again when the inner SQL string executes. Even better than manual quote doubling for values is parameterizing with sp_executesql:

-- Safer: pass data via parameters rather than concatenating single quotes
DECLARE @city NVARCHAR(100) = N'Cox''s Bazar';
DECLARE @sql NVARCHAR(MAX) = N'
    SELECT customer_id, customer_name, city
    FROM dbo.customers
    WHERE city = @p_city;';

EXEC sp_executesql
    @sql,
    N'@p_city NVARCHAR(100)',
    @p_city = @city;
  • If you must embed a string literal inside dynamic SQL, use QUOTENAME(@value, '''') or REPLACE(@value, '''', '''''')—though parameterized sp_executesql is safer and prevents SQL injection.

4. Safe Execution Checklist in T-SQL

  1. Use '' (two single quotes) for literal apostrophes/quotes—never \'.
  2. Use N'...' when storing or comparing Unicode text so characters are not lost to code-page conversion.
  3. Prefer parameterized sp_executesql over string concatenation when building dynamic SQL to avoid quote-escaping bugs and SQL injection.
  4. Test strings with leading, consecutive, or trailing quotes (like '''' for a single quote character) using SELECT before running INSERT or UPDATE.