159

I want to use quotation with escape character. How can I do to avoid the following error when one has a special character?

Unclosed quotation mark after the character string.

2

9 Answers 9

191

You can escape quotation like this:

select 'it''s escaped'

result will be

it's escaped
0
143

To escape ' you simly need to put another before: ''

As the second answer shows it's possible to escape single quote like this:

select 'it''s escaped'

result will be

it's escaped

If you're concatenating SQL into a VARCHAR to execute (i.e. dynamic SQL), then I'd recommend parameterising the SQL. This has the benefit of helping guard against SQL injection plus means you don't have to worry about escaping quotes like this (which you do by doubling up the quotes).

e.g. instead of doing

DECLARE @SQL NVARCHAR(1000)
SET @SQL = 'SELECT * FROM MyTable WHERE Field1 = ''AAA'''
EXECUTE(@SQL)

try this:

DECLARE @SQL NVARCHAR(1000)
SET @SQL = 'SELECT * FROM MyTable WHERE Field1 = @Field1'
EXECUTE sp_executesql @SQL, N'@Field1 VARCHAR(10)', 'AAA'
4
  • 32
    Why is this the accepted answer? It doesn't answer the question. Commented Feb 5, 2018 at 13:11
  • 5
    @PeterMoore Either the OP would have used the 1st part of my answer (doubling up the quotes, as per other answers below), or would have used the preferred approach I recommended for building up a SQL query in a string variable - to use parameterized SQL. Either way, both are answers to the question
    – AdaTheDev
    Commented Feb 5, 2018 at 14:48
  • It doesn't answer the question. Sometimes user need ODBC connection which means you can only use pure SQL.
    – Tony
    Commented Dec 23, 2019 at 15:38
  • Edited answer to more it more clear and better fit the question
    – Revious
    Commented Apr 7, 2020 at 13:14
65

You can define your escape character, but you can only use it with a LIKE clause.

Example:

SELECT columns FROM table
WHERE column LIKE '%\%%' ESCAPE '\'

Here it will search for % in whole string and this is how one can use ESCAPE identifier in SQL Server.

26

You need to just replace ' with '' inside your string

SELECT colA, colB, colC
FROM tableD
WHERE colA = 'John''s Mobile'

You can also use REPLACE(@name, '''', '''''') if generating the SQL dynamically

If you want to escape inside a like statement then you need to use the ESCAPE syntax

It's also worth mentioning that you're leaving yourself open to SQL injection attacks if you don't consider it. More info at Google or: http://it.toolbox.com/wiki/index.php/How_do_I_escape_single_quotes_in_SQL_queries%3F

2
  • and yet answers by dugokontov or RichardPianka don't have any similar -1?
    – Seph
    Commented Sep 21, 2014 at 12:16
  • @MichaelMunsey try it for yourself: select ' returns the error Unclosed quotation mark after the character string ''. Nowhere in my answer do I use " only two ', not sure why mine is the only answer with down votes.
    – Seph
    Commented Aug 19, 2015 at 12:23
15

Escaping quotes in MSSQL is done by a double quote, so a '' or a "" will produce one escaped ' and ", respectively.

4

If you want to escape user input in a variable you can do like below within SQL

  Set @userinput = replace(@userinput,'''','''''')

The @userinput will be now escaped with an extra single quote for every occurance of a quote

1
  • In my cae I had to do: str_replace("'", "''", @userinput) Commented May 7 at 10:18
4
WHERE username LIKE '%[_]d';            -- @Lasse solution
WHERE username LIKE '%$_d' ESCAPE '$';
WHERE username LIKE '%^_d' ESCAPE '^';

FROM: SQL Server Escape an Underscore

-1

To keep the code easy to read, you can use square brackets [] to quote the string containing ' or vice versa .

2
  • This is incorrect. Brackets work on illegal characters in field, table, or schema names. Commented Jul 15, 2019 at 23:14
  • Yeah, you right, its for the object names, not string contents. I must read the question wrong.
    – Ben
    Commented Jul 16, 2019 at 0:57
-1

You could use the **\** character before the value you want to escape e.g insert into msglog(recipient) values('Mr. O\'riely') select * from msglog where recipient = 'Mr. O\'riely'

Not the answer you're looking for? Browse other questions tagged or ask your own question.