Skip site navigation (1) Skip section navigation (2)

Re: Postgres Wishlist

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Donald Kerr <donald(dot)kerr(at)dkerr(dot)co(dot)uk>
Cc: "'Steve Crawford'" <scrawford(at)pinpointresearch(dot)com>, "'Tom Lane'" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Postgres Wishlist
Date: 2010-11-13 10:00:30
Message-ID: 3A9A5BCE-1914-4B69-B185-57AFC67BDC0A@seespotcode.net (view raw or flat)
Thread:
Lists: pgsql-novice
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




In response to

Responses

pgsql-novice by date

Next:From: Mladen GogalaDate: 2010-11-13 17:59:39
Subject: Re: Postgres Wishlist
Previous:From: Donald KerrDate: 2010-11-13 08:53:47
Subject: Re: Postgres Wishlist

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group