[freetds] apostrophes in strings

Arnar Birgisson ArnarB at oddi.is
Mon Jun 23 09:59:51 EDT 2003

Hello there..

Doubling the quote is the standard way and it works from php in my

$result = mssql_query("select 'it''s a giraffe!' ");

I don't use pear, you should check if Pear does some mangling with your
string before sending it off to SQL Server.

For future reference: PHP has very similar string represtentation as
Perl, double quoted strings are expanded for variables (f.x. "you are
number $i") and expressions inside {} (f.x. "my name is
{$userObject->somevariable['array index']}"), as well as
escape-sequences such as \n,\t etc. Single quoted strings are not
expanded, except for \'.


>>> LowdenJK at bernstein.com 20.6.2003 17:56:31 >>>
> 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
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
their own.  I don't use PHP, so I don't know how strings are expressed,
for instance Perl wouldn't permit doubling up the quote; the
character can't appear in the string without being escaped. 
Perl is very flexible on the point, accepting as a delimiter single or
double quotes, or even any arbitrary character prefixed with "qq".)  


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

FreeTDS mailing list
FreeTDS at lists.ibiblio.org 

More information about the FreeTDS mailing list