"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:
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/
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
pgsql-novice by date
|Next:||From: Steve Crawford||Date: 2010-11-12 21:47:34|
|Subject: Re: Postgres Wishlist|
|Previous:||From: LazyTrek||Date: 2010-11-12 21:23:53|
|Subject: Re: login error - Warning: pg_connect(): Unable to connect
to PostgreSQL server: FATAL: password authentication failed for user "kamik"|