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

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 (view raw or flat)
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

pgsql-novice by date

Next:From: Steve CrawfordDate: 2010-11-12 21:47:34
Subject: Re: Postgres Wishlist
Previous:From: LazyTrekDate: 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"

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