Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 703
MukeshAnAlsoRan Avatar asked Jan 19 '12 17:01

MukeshAnAlsoRan


People also ask

How do you escape a double quote in SQL query?

Use two single quotes to escape them in the sql statement. The double quotes should not be a problem: SELECT 'How is my son''s school helping him learn? "Not as good as Stack Overflow would!"'

How do you update data with single quotes in SQL?

The short answer is to use two single quotes - '' - in order for an SQL database to store the value as ' .

Are single and double quotes the same in SQL?

Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes generally aren't used in SQL, but that can vary from database to database. Stick to using single quotes.


Video Answer


2 Answers

You can escape the quotes with a backslash:

"I asked my son's teacher, \"How is my son doing now?\"" 
like image 195
Russell Zahniser Avatar answered Oct 07 '22 01:10

Russell Zahniser


Use two single quotes to escape them in the sql statement. The double quotes should not be a problem:

SELECT 'How is my son''s school helping him learn?  "Not as good as Stack Overflow would!"' 

Print:

How is my son's school helping him learn? "Not as good as Stack Overflow would!"

like image 33
mclark1129 Avatar answered Oct 07 '22 03:10

mclark1129