LPAD & RPAD with parameter

1.7k views Asked by At

I have a problem, I have a function in postgres, to generate headers that will be used to generate a more complex report.

There all good. For the structure of the header, I leave spaces left or right according to the field itself, this is achieved on the lpad and rpad.

The syntax is basically lpad or rpad (string, length, pad_string). up there all right.

The complicated thing is that the "length", which should be an integer number, I must do it for a variable value of a field in a control table.

the value is variable, so I made a function that gives me back the value of it.

I have tried to do it this way:

        SELECT c_transac||
           lpad(c_tr_count,(SELECT crd.get_size_field('CRD',c_transac,'TRANSACTION-SEQUENCE')),' ')||
           lpad(c_rc_count,8,' ')||
           rpad(to_char(c_ctac_correlativo,'9999'),10,' ')||
           rpad(ti.type_right,3,' ')||
           lpad(ct.peri_codigo,8,' ')||
           lpad(ct.peri_codigo,8,' ')||
           lpad(v_pcod_scd_dest,11,' ')||
           rpad(v_sigla_dest,45,' ')||
           lpad('0',8,' ')||
           lpad(to_char(c_decimal,'9'),1,' ')||
           lpad(to_char(c_percentage,'9'),1,' ')||
           rpad(c_version_crd,5,' ')||
           rpad(c_currency,3,' '),
           ti.type_right
    INTO v_linea_sdn, c_type_right
    FROM crd.tidav ti, public.ctact ct
    WHERE to_number(ti.tida_codigo, '9') = c_tida_codigo
    AND ct.ctac_correlativo = c_ctac_correlativo;

The function that I call returns a numerical value, and it gives me the following error:

ERROR: there is no lpad function (character varying, numeric, unknown)
LINE 2: lpad (c_tr_count, (SELECT crd.get_size_field ('CRD', c_tra ...
1

There are 1 answers

1
Laurenz Albe On

Add a type cast:

lpad(
   c_tr_count,
   CAST(
      crd.get_size_field('CRD',c_transac,'TRANSACTION-SEQUENCE')
      AS integer
   ),
   ' '
)