Escaping special characters

Lists: pgsql-general
From: Neanderthelle Jones <elle(at)view(dot)net(dot)au>
To: PGSQL Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Escaping special characters
Date: 2009-03-17 12:05:20
Message-ID: Pine.LNX.4.64.0903172219120.2728@calypso.view.net.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

About the string "Smith \& Jones".

According to the documentation,

INSERT INTO thing (name) VALUES ('Smith E'\\'& Jones');

must work. But it doesn't. So, double the enclosed quotes:

INSERT INTO thing (name) VALUES ('Smith E''\\''& Jones');

Doesn't.

It works fine, but with a warning, as

INSERT INTO thing (name) VALUES ('Smith \\& Jones');

But it mightn't if I upgrade from 8.2.3. Deprecated. Can't risk it.
So 40,000 years from now I'll be on 8.2.3.

Granted, I'm not very bright. Would appreciate your help.

--Elle


From: Richard Huxton <dev(at)archonet(dot)com>
To: Neanderthelle Jones <elle(at)view(dot)net(dot)au>
Cc: PGSQL Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Escaping special characters
Date: 2009-03-17 12:30:47
Message-ID: 49BF97F7.2060401@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Neanderthelle Jones wrote:
> About the string "Smith \& Jones".
>
> According to the documentation,
>
> INSERT INTO thing (name) VALUES ('Smith E'\\'& Jones');
>
> must work. But it doesn't.

I think you'll find the documentation says to use:
SELECT E'Smith \\& Jones';

Note that the "E" precedes the quoted string, it isn't embedded in it.
If there's an example in the docs that looks like yours, that's a bug.

> But it mightn't if I upgrade from 8.2.3. Deprecated. Can't risk it.
> So 40,000 years from now I'll be on 8.2.3.

Doubtful - you're missing 9 releases of bugfixes already. Probably find
all your data gets eaten by a bug long before then. Read the release
notes for 8.2.x and upgrade to 8.2.<latest> at your earliest convenience.

--
Richard Huxton
Archonet Ltd


From: Thom Brown <thombrown(at)gmail(dot)com>
To: Neanderthelle Jones <elle(at)view(dot)net(dot)au>
Cc: PGSQL Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Escaping special characters
Date: 2009-03-17 12:31:36
Message-ID: bddc86150903170531y24430952n1472a0452b4cf4be@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

>
> According to the documentation,
>
> INSERT INTO thing (name) VALUES ('Smith E'\\'& Jones');
>
> must work. But it doesn't. So, double the enclosed quotes:
>
> INSERT INTO thing (name) VALUES ('Smith E''\\''& Jones');
>
> Doesn't.
>
> It works fine, but with a warning, as
>
> INSERT INTO thing (name) VALUES ('Smith \\& Jones');
>
> But it mightn't if I upgrade from 8.2.3. Deprecated. Can't risk it.
> So 40,000 years from now I'll be on 8.2.3.
>

I could be wrong, but shouldn't it be:

INSERT INTO thing (name) VALUES ('Smith E'\\& Jones');

I'm not sure why you're including an extra single or double-quote in the
string.

Regards

Thom


From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Escaping special characters
Date: 2009-03-17 12:36:48
Message-ID: 20090317123648.GA32672@frubble.xen.chris-lamb.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Tue, Mar 17, 2009 at 10:35:20PM +1030, Neanderthelle Jones wrote:
> About the string "Smith \& Jones".
>
> According to the documentation,
>
> INSERT INTO thing (name) VALUES ('Smith E'\\'& Jones');
>
> must work. But it doesn't.

You're putting things in the wrong places! The "E" says that the
following literal is using C style escaping. I.e. you want to say:

E'Smith \\& Jones'

Hope that helps!

--
Sam http://samason.me.uk/


From: Thom Brown <thombrown(at)gmail(dot)com>
To: Neanderthelle Jones <elle(at)view(dot)net(dot)au>
Cc: PGSQL Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Escaping special characters
Date: 2009-03-17 12:39:50
Message-ID: bddc86150903170539r6f4e885at47c74a6f75ff8736@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

2009/3/17 Thom Brown <thombrown(at)gmail(dot)com>

>
> I could be wrong, but shouldn't it be:
>
> INSERT INTO thing (name) VALUES ('Smith E'\\& Jones');
>
> I'm not sure why you're including an extra single or double-quote in the
> string.
>
> Regards
>
> Thom
>

Sorry, (damn copy & paste). I meant:

INSERT INTO thing (name) VALUES (E'Smith \\& Jones');

Thom


From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "Neanderthelle Jones" <elle(at)view(dot)net(dot)au>
Cc: "PGSQL Mailing List" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Escaping special characters
Date: 2009-03-17 12:48:18
Message-ID: b9b176fc-de94-423b-b3ec-b1ba0025766f@mm
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Neanderthelle Jones wrote:

> About the string "Smith \& Jones".
>
> According to the documentation,
>
> INSERT INTO thing (name) VALUES ('Smith E'\\'& Jones');
>
> must work. But it doesn't. So, double the enclosed quotes:
>
> INSERT INTO thing (name) VALUES ('Smith E''\\''& Jones');

The E can't be inside the string, it must appear before the quote
starting the string.

But first, you need to choose a setting for
standard_conforming_strings, especially if you're concerned with
compatibility against future versions. Either your session has
standard_conforming_strings set to ON or set to OFF. This is what
defines which characters have to be quoted and how.

if OFF you must escape the backslash:
test=> set standard_conforming_strings=off;
SET
test=> select E'Smith \\& Jones';
?column?
----------------
Smith \& Jones
(1 row)

if ON you don't:
test=> set standard_conforming_strings=on;
SET
test=> select 'Smith \& Jones';
?column?
----------------
Smith \& Jones
(1 row)

ON is supposed to become the default at some point in the future.

Cordialement,
--
Daniel


From: Neanderthelle Jones <elle(at)view(dot)net(dot)au>
To: PGSQL Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: Escaping special characters
Date: 2009-03-17 15:17:50
Message-ID: Pine.LNX.4.64.0903180146090.3317@calypso.view.net.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Tue, 17 Mar 2009, Sam Mason wrote:

> You're putting things in the wrong places! The "E" says that the
> following literal is using C style escaping. I.e. you want to say:
>
> E'Smith \\& Jones'

Thanks. Now I understand.

Elle.