Passing a String with special character as an input

Started by akp geekover 15 years ago3 messagesgeneral
Jump to latest
#1akp geek
akpgeek@gmail.com

Hi -

This is related to my earlier post. For the function I am passing
a string. But the string some time has a single quote inside the string like
"IT's a String Test" , How can I handle that, can you please help?

CREATE OR REPLACE FUNCTION test_repl(x character varying)
RETURNS character varying AS
$BODY$
DECLARE
ret_var varchar(4000);
a record;
begin
ret_var := x;

for a in select * from lookup
loop
ret_var := replace(ret_var,a.code,a.codeword);
end loop;
return ret_var;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;
ALTER FUNCTION test_repl(character varying) OWNER TO postgres;

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: akp geek (#1)
Re: Passing a String with special character as an input

Hello

2010/11/23 akp geek <akpgeek@gmail.com>:

Hi -
           This is related to my earlier post. For the function I am passing
a string. But the string some time has a single quote inside the string like
"IT's a String Test" , How can I handle that, can you please help?

on stored procedure level you can do nothing

in SQL level, you have to duble quotes

like INSERT INTO data VALUES('Peter''s book');

regards

Pavel Stehule

Show quoted text

CREATE OR REPLACE FUNCTION test_repl(x character varying)
  RETURNS character varying AS
$BODY$
DECLARE
ret_var varchar(4000);
a record;
begin
ret_var := x;
for a in select * from lookup
loop
  ret_var := replace(ret_var,a.code,a.codeword);
end loop;
return ret_var;
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;
ALTER FUNCTION test_repl(character varying) OWNER TO postgres;

#3John R Pierce
pierce@hogranch.com
In reply to: akp geek (#1)
Re: Passing a String with special character as an input

On 11/23/10 12:34 PM, akp geek wrote:

Hi -

This is related to my earlier post. For the function I am
passing a string. But the string some time has a single quote inside
the string like "IT's a String Test" , How can I handle that, can you
please help?

at the SQL level, pass it as a parameter.

like, in perl...

my $sth = $dbh->prepare('select test_repl(?);');
$sth->execute("It's a String Test");

(or my $sth->execute('It\'s a String Test'); ...)