pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedPL/pgsql declaration of string / bit / number with given (variable!) length
2+ messages / 2 participants
[nested] [flat]
* PL/pgsql declaration of string / bit / number with given (variable!) length
@ 2007-02-05 16:02 sneumann <sneumann@ipb-halle.de>
0 siblings, 1 reply; 2+ messages in thread
From: sneumann @ 2007-02-05 16:02 UTC (permalink / raw)
To: pgsql-sql
Hi,
I have a PL/pgsql function that creates a certain bit string
based on the parameters. Things work fine if I use bit(10)
throughout the function. Now I'd like to return a bit string
with the given size "len", but that breaks with a syntax error:
ret := B'0'::bit(len);
LINE 1: SELECT B'0'::bit( $1 )
^
Any suggestion how to return a (bit) string
of user-defined length ?
Thanks in advance,
Yours,
Steffen
CREATE OR REPLACE FUNCTION unarystr(
value integer,
len integer
) RETURNS bit(10) AS $$
DECLARE
ret bit(10);
one bit(10);
BEGIN
ret := B'0'::bit(10);
one := B'1'::bit(10);
FOR num IN 0..value-1 LOOP
ret := ret | (one >>num);
END LOOP;
RETURN ret;
END;
$$ LANGUAGE plpgsql;
--
IPB Halle AG Massenspektrometrie & Bioinformatik
Dr. Steffen Neumann http://www.IPB-Halle.DE
Weinberg 3 http://msbi.bic-gh.de
06120 Halle New phone number !
Tel. +49 (0) 345 5582 - 1470
+49 (0) 345 5582 - 0
sneumann(at)IPB-Halle.DE Fax. +49 (0) 345 5582 - 1409
Attachments:
signature.asc (application/pgp-signature, 190B) download
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: PL/pgsql declaration of string / bit / number with given (variable!) length
@ 2007-02-06 09:45 Richard Huxton <dev@archonet.com>
parent: sneumann <sneumann@ipb-halle.de>
0 siblings, 0 replies; 2+ messages in thread
From: Richard Huxton @ 2007-02-06 09:45 UTC (permalink / raw)
To: sneumann <sneumann@ipb-halle.de>; +Cc: pgsql-sql
sneumann wrote:
> Hi,
>
> I have a PL/pgsql function that creates a certain bit string
> based on the parameters. Things work fine if I use bit(10)
> throughout the function. Now I'd like to return a bit string
> with the given size "len", but that breaks with a syntax error:
>
> ret := B'0'::bit(len);
> LINE 1: SELECT B'0'::bit( $1 )
> ^
>
> Any suggestion how to return a (bit) string
> of user-defined length ?
Hmm - not sure it's possible (even theoretically) for a function to have
multiple return types. Could you return a "bit varying" and cast it when
received?
--
Richard Huxton
Archonet Ltd
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2007-02-06 09:45 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-02-05 16:02 PL/pgsql declaration of string / bit / number with given (variable!) length sneumann <sneumann@ipb-halle.de>
2007-02-06 09:45 ` Re: PL/pgsql declaration of string / bit / number with given (variable!) length Richard Huxton <dev@archonet.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox