Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Escaping special chars in PL/SQL Postgres

Posted on 2004-10-21
6
Medium Priority
?
2,499 Views
Last Modified: 2012-06-27
How do I escape special chars in PL/SQL in postgres.  I tried to escape the apostrophe into XML escape sequence.  I did something like this and got an error:

vAlias := replace(alias, '''', ''');

The error I got:  ERROR: syntax error at or near "&"



0
Comment
Question by:tlchu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 22

Expert Comment

by:earth man2
ID: 12378700
PL/SQL does not exist in the postgresql world.  plplsql is wot it is called !

Apostrophe  living hell can be avoided by using a different format by using for example double dollar $$

CREATE OR REPLACE FUNCTION test1( varchar ) RETURNS varchar AS $$
begin
   return replace( $1, '''', '&apos' );
end;
$$ LANGUAGE plpgsql;

select test1('O''Connor');
    test1
--------------
 O&aposConnor

If you really need to use a double dollar somewhere in the code you can use some other unique character sequence.
0
 
LVL 22

Expert Comment

by:earth man2
ID: 12378752
CREATE OR REPLACE FUNCTION test1( varchar ) RETURNS varchar AS $$
begin
   return replace( $1, '''', ''' );
end;
$$ LANGUAGE plpgsql;

as opposed to - well I'm not going to even try to count those apostrophes !

CREATE OR REPLACE FUNCTION test1( varchar ) RETURNS varchar AS '
begin
   return replace( $1, '''''''', ''''' );
end;
' LANGUAGE plpgsql;
0
 
LVL 22

Expert Comment

by:earth man2
ID: 12378759
plpgsql is wot it is called
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:tlchu
ID: 12382931

I'd like to know what is the simple syntax for doing apostrophe replacement without the complication of the two functions you've given above.  My original statement:

vAlias := replace(alias, '''', ''');

Can it be rewritten in a way that works?  Thanks.
0
 
LVL 22

Expert Comment

by:earth man2
ID: 12383370
I'm not suggesting you encapsulate the replace operation in a function they are just the simplest approach to produce a testable software unit.

Do you understand the use of $$ to delimit the plpgsql code instead of using apostrophe's ?

If you use $$

vAlias := replace(alias, '''', ''');

otherwise

vAlias := replace( $1, '''''''', ''''' );
0
 
LVL 22

Accepted Solution

by:
earth man2 earned 2000 total points
ID: 12383381
otherwise

vAlias := replace( alias, '''''''', ''''' );
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question