Escape Character in SQL Server

Sql ServerEscapingChar

Sql Server Problem Overview


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.

Sql Server Solutions


Solution 1 - Sql Server

You can escape quotation like this:

select 'it''s escaped'

result will be

it's escaped

Solution 2 - Sql Server

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'

Solution 3 - Sql Server

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.

Solution 4 - Sql Server

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

Solution 5 - Sql Server

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

Solution 6 - Sql Server

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

Solution 7 - Sql Server

WHERE username LIKE '%[_]d';            -- @Lasse solution
WHERE username LIKE '%$_d' ESCAPE '$';
WHERE username LIKE '%^_d' ESCAPE '^';

FROM: https://stackoverflow.com/questions/5821/sql-server-escape-an-underscore

Solution 8 - Sql Server

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'

Solution 9 - Sql Server

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionesquareView Question on Stackoverflow
Solution 1 - Sql ServerdugokontovView Answer on Stackoverflow
Solution 2 - Sql ServerAdaTheDevView Answer on Stackoverflow
Solution 3 - Sql ServerAniket AView Answer on Stackoverflow
Solution 4 - Sql ServerSephView Answer on Stackoverflow
Solution 5 - Sql ServerRichard PiankaView Answer on Stackoverflow
Solution 6 - Sql ServerNayajivView Answer on Stackoverflow
Solution 7 - Sql ServerR.AlonsoView Answer on Stackoverflow
Solution 8 - Sql Serverparadox earthlingView Answer on Stackoverflow
Solution 9 - Sql ServerBenView Answer on Stackoverflow