views:

55

answers:

2

Here's a very easy question for someone :)

Trying to update an SQL column with the following:

UPDATE [NameOfTable]
SET [HtmlContent] = 'a href="/sell-your-boat/"'
WHERE HtmlID = 123456

But am getting the following error message: Incorrect syntax near '/'.

I know it's because I need to escape the / character but hitting my head against the wall trying to find the answer because I am aware it's probably very simple!

Thank you

+1  A: 

You don't need to escape slashes in a string in SQL. The only chracter that you need to escape is apostrophe (').

There is nothing wrong with the query that you are showing, so the only explanation is that the code that you are actually running does not look like that.

It doesn't make sense to have HTML-encoded quotation marks around a href attribute, so my guess is that the HTML code actually looks something like this:

<a href='/sell-your-boat/'>

Any apostrophes in the text would have to be encoded as double apostrophes when you put it in a string literal in the SQL code.

I don't know where the query is executed from, but a parameterised query would be preferrable if possible, as then you don't have to escape the text yourself, you just assign the text to the property value.

Guffa
Yep my bad. Had one apostrophes in the string I was trying to insert and just skimmed over it when I was copying and pasting. Replaced it with a double apostrophe and BINGO all green. Thank you everyone :)
timothyclifford
+1  A: 

Like all the comments above, youd don't need to escape the /

I just did a quick sql test in sql server 2005 and didn't get an error message (see below) We'll probably need more information than what you provided. Are you running this in Management studio, or is this sql being called in a .NET application, etc...

create table test (htmlid int, htmlcontent varchar(516))

insert into test select 123456 as htmlid, 'test' as htmlcontent


update test
set htmlcontent = 'a href=&quot;/sell-your-boat/&quot;'
where htmlid = 123456

select * from test where htmlid = 123456

drop table test

my output

123456  a href=&quot;/sell-your-boat/&quot;
clyc