[freetds] apostrophes in strings

Lowden, James K LowdenJK at bernstein.com
Fri Jun 20 13:56:31 EDT 2003


> From: Rusty Wright [mailto:rusty at socrates.Berkeley.EDU]
> Sent: June 20, 2003 12:55 PM
> 
> Is there a way to "quote" an apostrophe or single quote for MS SQL
> when inserting a string with a single quote in it?  Or some way to
> embed it as part of the string (for example, like url encoding)?
> 
> I'm using PHP and Pear.  I've tried doubling the single quote, and
> preceding it with a backslash; neither way works.

You should be able to double the single quote:

1> select 'It''s alright. Da doo du da doo du....'
2> go

 -------------------------------------
 It's alright. Da doo du da doo du....

That's the only standard way, afaik.  It works in tsql and sqsh.  If it
doesn't work in PHP, it's a bug.  You could work around it with:

1> select 'It' + char(39) + 's alright'
2> go

 ------------
 It's alright

You can also alternate quoting characters (select "It's alright").  

Note these are SQL quoting rules, which are weird.  Other languages have
their own.  I don't use PHP, so I don't know how strings are expressed, but
for instance Perl wouldn't permit doubling up the quote; the delimiting
character can't appear in the string without being escaped.  (Fortunately
Perl is very flexible on the point, accepting as a delimiter single or
double quotes, or even any arbitrary character prefixed with "qq".)  

HTH.  

--jkl
-----------------------------------------
The information contained in this transmission may contain privileged and confidential information and is intended only for the use of the person(s) named above. If you are not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, any review, dissemination, distribution or duplication of this communication is strictly prohibited. If you are not the intended recipient, please contact the sender immediately by reply e-mail and destroy all copies of the original message. Please note that we do not accept account orders and/or instructions by e-mail, and therefore will not be responsible for carrying out such orders and/or instructions.





More information about the FreeTDS mailing list