Subject Re: [firebird-support] Using strings with Firebird 1.5 ! FAQs
Author Helen Borrie
At 01:23 PM 3/05/2005 +0000, you wrote:
>It is possible to use a procedure to parse an array from a field
>database(a varchar for example) accessing to the letters like an array
>of chars?
>
>My objective is to parse a field of a database to get some information
>inside!. i'll reproduce a part of
>representative code to explain the idea:
>
>
>/* wrong procedure */
>CREATE PROCEDURE PR_EXTREU_DATE (pi_CAD varchar(240))
>RETURNS (po_ADRECA VARCHAR(200))
>AS
>DECLARE VARIABLE v_COMPTADOR SMALLINT;
>begin
> po_ADRECA = '';
> v_COMPTADOR = 0;
>
> while (pi_CAD[:v_COMPTADOR] <> '*') do
> begin
> po_ADRECA = po_ADRECA || pi_CAD[:v_COMPTADOR];
> v_COMPTADOR = v_COMPTADOR + 1;
> end
>
> suspend;
>end
>
>In this procedure, the intention, is pass a field of a database, and
>parse it into another string.

Use SUBSTRING():

CREATE PROCEDURE PR_EXTREU_DATE (pi_CAD varchar(240))
RETURNS (po_ADRECA VARCHAR(200))
AS
declare currentchar char = '';
declare terminated char = 'F';
begin
po_ADRECA = '';

while (terminated = 'F') do
begin
currentchar = SUBSTRING(pi_CAD from 1 for 1);
if (currentchar is null or currentchar = '') then
begin
terminated = 'T';
LEAVE;
end
po_ADRECA = po_ADRECA || currentchar;
currentchar = '';
pi_CAD = SUBSTRING(pi_CAD from 2);
end
suspend;
end

Watch out for your declarations here. You are passing in a varchar(240),
of which 239 characters might be transferred to po_ADRECA, which is only
varchar(200): an overflow situation. Overflows cause exceptions.

./hb