Re: Postgres Wishlist

From: Mladen Gogala <mladen(dot)gogala(at)vmsinfo(dot)com>
To: Michael Glaesemann <grzm(at)seespotcode(dot)net>
Cc: Donald Kerr <donald(dot)kerr(at)dkerr(dot)co(dot)uk>, 'Steve Crawford' <scrawford(at)pinpointresearch(dot)com>, 'Tom Lane' <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Postgres Wishlist
Date: 2010-11-13 18:07:58
Message-ID: 4CDED3FE.2080809@vmsinfo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Michael Glaesemann wrote:
> On Nov 13, 2010, at 3:46 , Donald Kerr wrote:
>
>
>> Steve,
>>
>> That works a treat:
>>
>> -----------------------------------
>> CREATE OR REPLACE FUNCTION hex_to_int(varchar) RETURNS integer AS '
>> DECLARE
>> h alias for $1;
>> exec varchar;
>> curs refcursor;
>> res int;
>> BEGIN
>> exec := ''SELECT x'''''' || h || ''''''::int'';
>> OPEN curs FOR EXECUTE exec;
>> FETCH curs INTO res;
>> CLOSE curs;
>> return res;
>> END;'
>> LANGUAGE 'plpgsql'
>> IMMUTABLE
>> STRICT;
>>
>
> That's really arcane. Much more simply:
>
> CREATE FUNCTION
> hex2dec(in_hex TEXT)
> RETURNS INT
> IMMUTABLE
> STRICT LANGUAGE sql AS $body$
> SELECT CAST(CAST(('x' || CAST($1 AS text)) AS bit(8)) AS INT);
> $body$;
>
> test=# select hex2dec('99');
> hex2dec
> ---------
> 153
> (1 row)
>
> Michael Glaesemann
> grzm seespotcode net
>
>
>
>
>
I think that something like this would be the easiest to read:

CREATE OR REPLACE FUNCTION hex2dec(text)
RETURNS int
AS $$
my $arg=shift;
return(hex($arg));
$$ LANGUAGE plperl;

It works like a charm:
CREATE OR REPLACE FUNCTION hex2dec(text)
RETURNS int
AS $$
my $arg=shift;
return(hex($arg));
$$ LANGUAGE plperl;

--
Mladen Gogala
Sr. Oracle DBA
1500 Broadway
New York, NY 10036
(212) 329-5251
www.vmsinfo.com

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Mladen Gogala 2010-11-13 19:03:27 Re: Postgres Wishlist
Previous Message Mladen Gogala 2010-11-13 17:59:39 Re: Postgres Wishlist