Showing posts with label apostrophe. Show all posts
Showing posts with label apostrophe. Show all posts

Monday, March 26, 2012

escaping an apostrophe in MSSQL

Hi, i'm having problems executing the following

SET @.SQLAH = 'SELECT sub_id WHERE '
SET @.SQLAH = @.SQLAH + 'VENUE_TYPE = Hotel'
EXEC(@.SQLAH)

Its getting stuck at Hotel. I realise that it should include an apostrophe either side like so:

..
SET @.SQLAH = @.SQLAH + 'VENUE_TYPE = 'Hotel' '
..

But this escapes the string, how would i escape an apostrophe in a string?

I thought maybe:

SET @.SQLAH = @.SQLAH + 'VENUE_TYPE = \'Hotel\' '

But no joy :confused:

ThanksAs far as I know MS SQL is using the ANSI standard for that: two single quotes:

SET @.SQLAH = @.SQLAH + 'VENUE_TYPE = 'Hotel'''

(Don't know if it works inside a procedure though)|||hi

nope, it doesnt work - i'm using a stored procedure
:eek:|||SET @.SQLAH = @.SQLAH + 'VENUE_TYPE = ''Hotel'' '-PatP|||thats the badger! cheerssql

Thursday, March 22, 2012

Escape Sequences

Is there an escape sequence available for the SQL Analyzer that will not
mung the `'` (apostrophe)
If I add it as a parameter in my code, it works, but I am curious to know
all the same.I'm not sure what you mean by "that will not mung the `'` (apostrophe)", but
if you pass a string which includes a single quote, you need to escape that
single quote with a single quote. I.e., double each single quote before
passing the string to SQL Server.
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"Wayne M J" <not@.home.nor.bigpuddle.com> wrote in message
news:%23ex1p5wvDHA.2444@.TK2MSFTNGP12.phx.gbl...
> Is there an escape sequence available for the SQL Analyzer that will not
> mung the `'` (apostrophe)
> If I add it as a parameter in my code, it works, but I am curious to know
> all the same.
>|||"Tibor Karaszi" <tibor.please_reply_to_public_forum.karaszi@.cornerstone.se>
wrote in message news:OMO5w8wvDHA.2072@.TK2MSFTNGP10.phx.gbl...
> I'm not sure what you mean by "that will not mung the `'` (apostrophe)",
but
> if you pass a string which includes a single quote, you need to escape
that
> single quote with a single quote. I.e., double each single quote before
> passing the string to SQL Server.
That is exactly what I was looking for.
Thanks.