Re: Postgres Wishlist

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Donald Kerr" <donald(dot)kerr(at)dkerr(dot)co(dot)uk>
Cc: "'Steve Crawford'" <scrawford(at)pinpointresearch(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: Postgres Wishlist
Date: 2010-11-12 21:38:29
Message-ID: 2946.1289597909@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

"Donald Kerr" <donald(dot)kerr(at)dkerr(dot)co(dot)uk> writes:
> I am trying to convert from hex to decimal and can do that successfully
> using the following code:

> SELECT x'FF'::integer;

> which outputs 255 and is exactly what I want.

What you're doing there is abusing the bit-string-literal syntax.
I think you could get access to the same behavior for a non-constant
input like this:

SELECT 'xff'::text::bit(8)::int;

or in practice

SELECT ('x' || some-string-variable)::bit(8)::int;

This is relying on some undocumented behavior of the bit-type input
converter, but I see no reason to expect that would break. A possibly
bigger issue is that it requires PG >= 8.3 since there wasn't a text
to bit cast before that.

> Thew data colum contains html color codes like "0099FF" and I want to
> convert these to, in this case, "0 153 255".

It seems very unlikely that you're going to get any built-in solution
to that. You don't have just a hex-to-decimal conversion problem; you
also have the problems of splitting the given string into three pieces
and deciding how you're going to represent the three-component result of
the conversion. And both of your choices there seem pretty arbitrary/
specialized.

I think your best bet would be to write a custom function that does what
you want. This'd likely be a one-liner in plperl, for example. You
could do it in plpgsql if you don't want to rely on plperl being
installed, but it'd be a tad more tedious.

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Steve Crawford 2010-11-12 21:47:34 Re: Postgres Wishlist
Previous Message LazyTrek 2010-11-12 21:23:53 Re: login error - Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: password authentication failed for user "kamik"