How to handle-escape both single and double quotes in an SQL-Update statement

MukeshAnAlsoRan picture MukeshAnAlsoRan · Jan 19, 2012 · Viewed 126.7k times · Source

I have this sample T-SQL query and trying this on SQL-Server-2008.

DECLARE nvarchar(1000) @wstring = "I asked my son's teacher, "How is my son doing now?"" 

UPDATE tablename SET columnname = ' " & @wstring & " ' where ... blah ... blah

I know that the above query will throw error.

So how do I handle-escape both single and double quotes in an SQL-Update statement.

Answer

Russell Zahniser picture Russell Zahniser · Jan 19, 2012

You can escape the quotes with a backslash:

"I asked my son's teacher, \"How is my son doing now?\""